跳到主要内容

Append to object

介绍

实现一个为接口添加一个新字段的类型。该类型接收三个参数,返回带有新字段的接口类型。

ts
type Test = { id: '1' }
type Result = AppendToObject<Test, 'value', 4> // expected to be { id: '1', value: 4 }
ts
type Test = { id: '1' }
type Result = AppendToObject<Test, 'value', 4> // expected to be { id: '1', value: 4 }
View on GitHub

起点

ts
/* _____________ Your Code Here _____________ */
 
type AppendToObject<T, U, V> = any
 
/* _____________ Test Cases _____________ */
 
type test1 = {
key: 'cat'
value: 'green'
}
 
type testExpect1 = {
key: 'cat'
value: 'green'
home: boolean
}
 
type test2 = {
key: 'dog' | undefined
value: 'white'
sun: true
}
 
type testExpect2 = {
key: 'dog' | undefined
value: 'white'
sun: true
home: 1
}
 
type test3 = {
key: 'cow'
value: 'yellow'
sun: false
}
 
type testExpect3 = {
key: 'cow'
value: 'yellow'
sun: false
moon: false | undefined
}
 
 
type cases = [
Expect<Equal<AppendToObject<test1, 'home', boolean>, testExpect1>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<AppendToObject<test2, 'home', 1>, testExpect2>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<AppendToObject<test3, 'moon', false | undefined>, testExpect3>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
ts
/* _____________ Your Code Here _____________ */
 
type AppendToObject<T, U, V> = any
 
/* _____________ Test Cases _____________ */
 
type test1 = {
key: 'cat'
value: 'green'
}
 
type testExpect1 = {
key: 'cat'
value: 'green'
home: boolean
}
 
type test2 = {
key: 'dog' | undefined
value: 'white'
sun: true
}
 
type testExpect2 = {
key: 'dog' | undefined
value: 'white'
sun: true
home: 1
}
 
type test3 = {
key: 'cow'
value: 'yellow'
sun: false
}
 
type testExpect3 = {
key: 'cow'
value: 'yellow'
sun: false
moon: false | undefined
}
 
 
type cases = [
Expect<Equal<AppendToObject<test1, 'home', boolean>, testExpect1>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<AppendToObject<test2, 'home', 1>, testExpect2>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<AppendToObject<test3, 'moon', false | undefined>, testExpect3>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
take the challenge

解决方案

Spoiler warning // Click to reveal answer
ts
type AppendToObject<T, U extends string, V> = T extends {[index: string]: any}
? {[K in keyof T|U]: K extends keyof T ? T[K] : V}
: T;
 
 
ts
type AppendToObject<T, U extends string, V> = T extends {[index: string]: any}
? {[K in keyof T|U]: K extends keyof T ? T[K] : V}
: T;
 
 
ts
// most popular
 
type AppendToObject<T, U extends keyof any, V> = {
[K in keyof T | U]: K extends keyof T ? T[K] : V;
};
 
ts
// most popular
 
type AppendToObject<T, U extends keyof any, V> = {
[K in keyof T | U]: K extends keyof T ? T[K] : V;
};
 
view more solutions