Type-Challenges_2 (easy 2 / 2)

YS C·2022년 4월 10일
0
post-thumbnail

easy

189 - Awaited

// qs
type MyAwaited = any
// answer
type MyAwaited<T extends Promise<any>> = T extends Promise<infer U> ? (U extends Promise<any> ? MyAwaited<U> : U) : never;

268 - If

// qs
type If<C, T, F> = any
// answer
type If<C extends boolean, T, F> = C extends true ? T : F;
// test-case
type A = If<true, 'a', 'b'>  // expected to be 'a'
type B = If<false, 'a', 'b'> // expected to be 'b'

533 - Concat

// qs
type Concat<T, U> = any
// answer
type Concat<T extends any[], U extends any[]> = [...T, ...U]
// test-case
type Result = Concat<[1], [2]> // expected to be [1, 2]

898 - Includes

// qs
type Includes<T extends readonly any[], U> = any;
// answer
type Includes<T extends readonly any[], U> = T extends [infer P, ...infer K] ? (Equal<P, U> extends true ? true : Includes<K, U>) : false;
// test-case
type isPillarMen = Includes<['Kars', 'Esidisi', 'Wamuu', 'Santana'], 'Dio'> // expected to be `false`
// 요 부분은 Equal 타입이 구현되어 있어야 동작... 애매...

3057 - Push

// qs
type Push<T, U> = any
// answer
type Push<T extends any[], U> = [...T, U]
// test-case
type Result = Push<[1, 2], '3'> // [1, 2, '3']

3060 - Unshift

// qs
type Unshift<T, U> = any
// answer
type Unshift<T extends any[], U> = [U, ...T]
// test-case
type Result = Unshift<[1, 2], 0> // [0, 1, 2,]

3312 - Parameters

// qs
type MyParameters<T extends (...args: any[]) => any> = any
// answer
type MyParameters<T extends (...args: any[]) => any> = T extends (...args: infer P) => any ? P : never;

0개의 댓글