Append to object
提出詳細
type Pretty<T> = { [K in keyof T]: T[K] } type AppendToObject<T, U extends string, V> = Pretty< T & { [K in U]: V } >;
提出日時 | 2023-08-11 11:10:29 |
---|---|
問題 | Append to object |
ユーザー | tekihei2317 |
ステータス | Accepted |
import type { Equal, Expect } from '@type-challenges/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 isMotherRussia: false | undefined } type cases = [ Expect<Equal<AppendToObject<test1, 'home', boolean>, testExpect1>>, Expect<Equal<AppendToObject<test2, 'home', 1>, testExpect2>>, Expect<Equal<AppendToObject<test3, 'isMotherRussia', false | undefined>, testExpect3>>, ]