Chainable options
MediumP2Coding
Implement the Chainable type for an options builder.
declare const config: Chainable
const result = config.option('foo', 123).option('name', 'x').get()
option(key, value)returns a new chainable whose accumulated object type haskey: typeof valueadded.get()returns the accumulated object type.- Using a key that was already set is a type error.
- The resulting object type must be one flat object type, not an intersection.
Tests your type must pass
declare const config: Chainable
const result1 = config
.option('foo', 123)
.option('name', 'type-challenges')
.option('bar', { value: 'Hello World' })
.get()
const result2 = config.option('a', true).get()
const result3 = config.get()
interface Result1 {
foo: number
name: string
bar: { value: string }
}
type cases = [
Expect<Equal<typeof result1, Result1>>,
Expect<Equal<typeof result2, { a: boolean }>>,
Expect<Equal<typeof result3, {}>>,
]
// @ts-expect-error the key 'foo' is already set
config.option('foo', 1).option('foo', 2)Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
TypeScript
Run and Submit type-check your solution against the tests. ⌘Enter runs, ⌘⇧Enter submits.