a: false };
type Merge<T1, T2> = ...
Merge<A, B> // { a: boolean }
как написать Merge?
type Merge<T extends object> = { [k in AllKeys<T>]: PickType<T, k>; };
type OptionalPropertyNames<T> = { [K in keyof T]-?: ({} extends { [P in K]: T[K] } ? K : never) }[keyof T]; type SpreadProperties<L, R, K extends keyof L & keyof R> = { [P in K]: L[P] | Exclude<R[P], undefined> }; type Id<T> = T extends infer U ? { [K in keyof U]: U[K] } : never type SpreadTwo<L, R> = Id< & Pick<L, Exclude<keyof L, keyof R>> & Pick<R, Exclude<keyof R, OptionalPropertyNames<R>>> & Pick<R, Exclude<OptionalPropertyNames<R>, keyof L>> & SpreadProperties<L, R, OptionalPropertyNames<R> & keyof L> >; type Spread<A extends readonly [...any]> = A extends [infer L, ...infer R] ? SpreadTwo<L, Spread<R>> : unknown type Foo = Spread<[{ a: string }, { a?: number }]> function merge<A extends object[]>(...a: [...A]) { return Object.assign({}, ...a) as Spread<A>; }
Обсуждают сегодня