type-challenges:Append to object

33 阅读1分钟

Append to object

问题描述

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

例如:

type Test = { id: '1' }
type Result = AppendToObject<Test, 'value', 4> // expected to be { id: '1', value: 4 }
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'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 test4 = {
  key: 'cow'
  value: 'yellow'
  sun: false
}
​
type testExpect4 = {
  key: 'cow'
  value: 'yellow'
  sun: false | undefined
}
type cases = [
  Expect<Equal<AppendToObject<test1, 'home', boolean>, testExpect1>>,
  Expect<Equal<AppendToObject<test2, 'home', 1>, testExpect2>>,
  Expect<Equal<AppendToObject<test3, 'moon', false | undefined>, testExpect3>>,
  Expect<Equal<AppendToObject<test4, 'sun',  false | undefined>, testExpect4>>
]
​
// ============= Your Code Here =============
// 答案1
type AppendToObject<T, U extends PropertyKey, V> = {
  [K in keyof T | U]: K extends keyof T ? T[K] : V
}
// 答案2
type Compute<T> = { [k in keyof T]: T[k] };
type AppendToObject<T, U extends PropertyKey, V> = Compute<Omit<T, U> & Record<U, V>>;
// 错误答案
type AppendToObject<T, U extends PropertyKey, V> = {
  [P in keyof T]: T[P]
} & {
  [K in U]: V
}
​
// 为什么这样可以
type a = { b: string }
type b = { d: number }
type e = a & b
let f: e = {
  b: '313',
  d: 33
}
let j = {
  b: '313',
  d: 33
}
type i = typeof f
type k = typeof j
type h = {
  b: string
  d: number
}
type g = Equal<i, e>
​

首先需要知道键名只有string | number | symbol 三种类型,可以用内置类型 PropertyKey 代替,在答案1中,K in keyof T | U 会遍历所有的键名,再构建一个条件类型,判断 K 是否继承自 keyof T 如果是,则键值为 T[K],否则为 V。但是这里有一个问题,那就是如果 U 在 泛型 T 的键名中该怎么办呢?如测试用例4,(这个测试用例是自己加的,原项目中没有),所以最好使用答案2,排除可能相同的键名,将两个对象联合。

暂时没有理解错误答案为什么不可以。