Skip to content

Latest commit

 

History

History
30 lines (19 loc) · 668 Bytes

File metadata and controls

30 lines (19 loc) · 668 Bytes

8. implement Extract<T, U>

Problem

https://bigfrontend.dev/typescript/implement-Extract-T-U

Problem Description

As the opposite of Exclude<T, K>, Extract<T, U> returns a type by extracting union members from T that are assignable to U.

Please implement MyExtract<T, U> by yourself.

type Foo = 'a' | 'b' | 'c';

type A = MyExtract<Foo, 'a'>; // 'a'
type B = MyExtract<Foo, 'a' | 'b'>; // 'a' | 'b'
type C = MyExtract<Foo, 'b' | 'c' | 'd' | 'e'>; // 'b' | 'c'
type D = MyExtract<Foo, never>; // never

Solution

type MyExtract<T, U> = T extends U ? T : never;