mirror of
https://github.com/type-challenges/type-challenges.git
synced 2025-12-08 19:06:13 +00:00
Chainable Options

by Anthony Fu @antfu
Chainable options are commonly used in Javascript. But when we switch to Typescript, can you properly type it?
In this challenge, you need to type an object or a class - whatever you like - to provide two function option(key, value) and get(). In option, you can extend the the current config type by the given key and value. We should about to access the final result via get.
For example
declare const config: Chainable
const result = config
.option('foo', 123)
.option('name', 'type-challenges')
.option('bar', { value: 'Hello World' })
.get()
// expect the type of result to be:
interface Result {
foo: number
name: string
bar: {
value: string
}
}
You don't need to write any js/ts logic to handle the problem - just in type level.
You can assume that key only accept string and the value and be anything - just leave it as-is. Same key won't be passed twice.