chainable options
介绍
实现一个可串联的构造器,你可以使用任意你喜欢的方式实现这个类型 - Interface, Type 或 Class 都行。你需要提供两个函数 option(key, value)
和 get()
。在 option
中你需要使用提供的 key 和 value 扩展当前的对象类型,通过 get
获取最终结果。
For example
ts
declare const config: Chainableconst result = config.option('foo', 123).option('name', 'type-challenges').option('bar', { value: 'Hello World' }).get()// 期望 result 的类型是:interface Result {foo: numbername: stringbar: {value: string}}
View on GitHubts
declare const config: Chainableconst result = config.option('foo', 123).option('name', 'type-challenges').option('bar', { value: 'Hello World' }).get()// 期望 result 的类型是:interface Result {foo: numbername: stringbar: {value: string}}
起点
ts
/* _____________ Your Code Here _____________ */// type Chainable = {// option(key: string, value: any): any// get(): any// }typeChainable = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Alike <typeofresult1 ,Expected1 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Alike <typeofresult2 ,Expected2 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Alike <typeofresult3 ,Expected3 >>,]declare consta :Chainable constresult1 =a .option ('foo', 123).option ('bar', {value : 'Hello World' }).option ('name', 'type-challenges').get ()constresult2 =a .option ('name', 'another name').option ('name', 'last name').get ()constresult3 =a .option ('name', 'another name').get ()typeExpected1 = {foo : numberbar : {value : string}name : string}typeExpected2 = {name : string}typeExpected3 = {name : number}
take the challengets
/* _____________ Your Code Here _____________ */// type Chainable = {// option(key: string, value: any): any// get(): any// }typeChainable = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Alike <typeofresult1 ,Expected1 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Alike <typeofresult2 ,Expected2 >>,Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.Expect <Alike <typeofresult3 ,Expected3 >>,]declare consta :Chainable constresult1 =a .option ('foo', 123).option ('bar', {value : 'Hello World' }).option ('name', 'type-challenges').get ()constresult2 =a .option ('name', 'another name').option ('name', 'last name').get ()constresult3 =a .option ('name', 'another name').get ()typeExpected1 = {foo : numberbar : {value : string}name : string}typeExpected2 = {name : string}typeExpected3 = {name : number}
解决方案
Spoiler warning // Click to reveal answer
ts
typeChainable = any
ts
typeChainable = any