跳到主要内容

chainable options

介绍

实现一个可串联的构造器,你可以使用任意你喜欢的方式实现这个类型 - Interface, Type 或 Class 都行。你需要提供两个函数 option(key, value)get()。在 option 中你需要使用提供的 key 和 value 扩展当前的对象类型,通过 get 获取最终结果。

For example

ts
declare const config: Chainable
const result = config
.option('foo', 123)
.option('name', 'type-challenges')
.option('bar', { value: 'Hello World' })
.get()
// 期望 result 的类型是:
interface Result {
foo: number
name: string
bar: {
value: string
}
}
ts
declare const config: Chainable
const result = config
.option('foo', 123)
.option('name', 'type-challenges')
.option('bar', { value: 'Hello World' })
.get()
// 期望 result 的类型是:
interface Result {
foo: number
name: string
bar: {
value: string
}
}
View on GitHub

起点

ts
/* _____________ Your Code Here _____________ */
 
// type Chainable = {
// option(key: string, value: any): any
// get(): any
// }
 
type Chainable = any
 
/* _____________ Test Cases _____________ */
type cases = [
Expect<Alike<typeof result1, Expected1>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Alike<typeof result2, Expected2>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Alike<typeof result3, Expected3>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
 
declare const a: Chainable
 
const result1 = a
.option('foo', 123)
.option('bar', { value: 'Hello World' })
.option('name', 'type-challenges')
.get()
 
const result2 = a
.option('name', 'another name')
.option('name', 'last name')
.get()
 
const result3 = a
.option('name', 'another name')
.get()
 
 
type Expected1 = {
foo: number
bar: {
value: string
}
name: string
}
 
type Expected2 = {
name: string
}
 
type Expected3 = {
name: number
}
ts
/* _____________ Your Code Here _____________ */
 
// type Chainable = {
// option(key: string, value: any): any
// get(): any
// }
 
type Chainable = any
 
/* _____________ Test Cases _____________ */
type cases = [
Expect<Alike<typeof result1, Expected1>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Alike<typeof result2, Expected2>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Alike<typeof result3, Expected3>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
 
declare const a: Chainable
 
const result1 = a
.option('foo', 123)
.option('bar', { value: 'Hello World' })
.option('name', 'type-challenges')
.get()
 
const result2 = a
.option('name', 'another name')
.option('name', 'last name')
.get()
 
const result3 = a
.option('name', 'another name')
.get()
 
 
type Expected1 = {
foo: number
bar: {
value: string
}
name: string
}
 
type Expected2 = {
name: string
}
 
type Expected3 = {
name: number
}
take the challenge

解决方案

Spoiler warning // Click to reveal answer
ts
 
type Chainable = any
 
ts
 
type Chainable = any
 
view more solutions