questions/00012-medium-chainable-options/README.ja.md
JavaScript では、チェイン可能なオプションがよく使われます。しかし、TypeScript に切り替えたとき、正しく型を付けることができますか?
この課題では、オブジェクトでもクラスでも何でもいいので、 option(key, value) と get() の 2 つの関数を提供する型を定義してください。option では、与えられたキーと値を使って現在の config の型を拡張できます。最終的な結果は get で取得することにしましょう。
例えば
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
}
}
この問題を解くために js/ts のロジックを書く必要はありません。型レベルのロジックだけを書いてください。
key は string のみを受け付け、value は任意の型を受け付けると仮定しても構いません。同じ key が 2 回渡されることはありません。
<a href="../../README.ja.md" target="_blank"></a> <a href="https://tsch.js.org/12/answer/ja" target="_blank"></a> <a href="https://tsch.js.org/12/solutions" target="_blank"></a> <!--info-footer-end-->