Go Back

Refer: Medium - Merge

Describe

Merge two types into a new type. Keys of the second type overrides keys of the first type.

Test Cases

code snippetCopytypescript
import { Equal, Expect } from '@type-challenges/utils' type Foo = { a: number b: string } type Bar = { b: number c: boolean } type cases = [ Expect< Equal< Merge<Foo, Bar>, { a: number b: number c: boolean } > >, ]

Solution

code snippetCopytypescript
// The intersection type does not seem to pass the type check! type Merge<F, S> = { [P in keyof S]: S[P] } & { [P in keyof F as P extends keyof S ? never : P]: F[P] } type Merge<F, S> = { [P in keyof (F & S)]: P extends keyof S ? S[P] : P extends keyof F ? F[P] : never }