์นดํ
๊ณ ๋ฆฌ ์์
ํ์ ์ฑ๋ฆฐ์ง [์ฌ์] ๋์ด๋ ์ค ์ธ์ฌ์ดํธ ์ ๋ฆฌ
chamroro
2025. 5. 27. 18:50
โ 1. 14 - First
๋ฌธ์ : ๋ฐฐ์ด์ ์ฒซ ๋ฒ์งธ ์์์ ํ์ ์ ์ถ์ถํ๋ผ.
type arr1 = ['a', 'b', 'c']
type arr2 = [3, 2, 1]
type head1 = First<arr1> // expected to be 'a'
type head2 = First<arr2> // expected to be 3
type cases = [
Expect<Equal<First<[3, 2, 1]>, 3>>,
Expect<Equal<First<[() => 123, { a: string }]>, () => 123>>,
Expect<Equal<First<[]>, never>>,
Expect<Equal<First<[undefined]>, undefined>>,
]
type errors = [
// @ts-expect-error
First<'notArray'>,
// @ts-expect-error
First<{ 0: 'arrayLike' }>,
]
type First<T extends any[]> = T extends [] ? never : T[0];
- ๋น ๋ฐฐ์ด์ด๋ฉด never, ์๋๋ฉด T[0]์ผ๋ก ์ฒซ ์์ ๋ฐํ
- ๋ฐฐ์ด ์ ์ฝ: T extends any[]
โ 2. 43 - Exclude
๋ฌธ์ : ์ ๋์ธ ํ์ T์์ U์ ํด๋นํ๋ ํ์ ์ ์ ๊ฑฐํ๋ผ.
type cases = [
Expect<Equal<MyExclude<'a' | 'b' | 'c', 'a'>, 'b' | 'c'>>,
Expect<Equal<MyExclude<'a' | 'b' | 'c', 'a' | 'b'>, 'c'>>,
Expect<Equal<MyExclude<string | number | (() => void), Function>, string | number>>,
]
type MyExclude<T, U> = T extends U ? never : T;
- ํต์ฌ: ์กฐ๊ฑด๋ถ ํ์ ์ ๋ถ์ฐ ์ฑ์ง
- 'a' | 'b' | 'c', 'a' → 'b' | 'c'
โ 3. 189 - Awaited
๋ฌธ์ : Promise ํ์ ์์ ๋ด๋ถ ๊ฐ์ ๊บผ๋ด๋ผ (์ฌ๊ท์ ์ผ๋ก).
type X = Promise<string>
type Y = Promise<{ field: number }>
type Z = Promise<Promise<string | number>>
type Z1 = Promise<Promise<Promise<string | boolean>>>
type T = { then: (onfulfilled: (arg: number) => any) => any }
type cases = [
Expect<Equal<MyAwaited<X>, string>>,
Expect<Equal<MyAwaited<Y>, { field: number }>>,
Expect<Equal<MyAwaited<Z>, string | number>>,
Expect<Equal<MyAwaited<Z1>, string | boolean>>,
Expect<Equal<MyAwaited<T>, number>>,
]
type MyAwaited<T extends PromiseLike<any>> = T extends PromiseLike<infer U>
? U extends PromiseLike<any>
? MyAwaited<U>
: U
: never;
- infer๋ก ๋ด๋ถ ํ์ ๊บผ๋
- ์ค์ฒฉ๋ Promise๋ ์ฌ๊ท๋ก ์ฒ๋ฆฌ
- PromiseLike ์ ์ฝ์ผ๋ก thenable ๋์
โ 4. 533 - Concat
๋ฌธ์ : ๋ฐฐ์ด T, U๋ฅผ ์ด์ด๋ถ์ธ ๊ฒฐ๊ณผ ํ์ ์ ๋ง๋ค๋ผ.
const tuple = [1] as const
type cases = [
Expect<Equal<Concat<[], []>, []>>,
Expect<Equal<Concat<[], [1]>, [1]>>,
Expect<Equal<Concat<typeof tuple, typeof tuple>, [1, 1]>>,
Expect<Equal<Concat<[1, 2], [3, 4]>, [1, 2, 3, 4]>>,
Expect<Equal<Concat<['1', 2, '3'], [false, boolean, '4']>, ['1', 2, '3', false, boolean, '4']>>,
]
// @ts-expect-error
type error = Concat<null, undefined>
type Concat<T extends readonly unknown[], U extends readonly unknown[]> = [...T, ...U];
- ํํ ์คํ๋ ๋๋ก ๊ฐ๋จํ ๋ณํฉ
โ 5. 898 - Includes
๋ฌธ์ : ๋ฐฐ์ด T์ ํ์ U๊ฐ ํฌํจ๋์ด ์๋์ง ํ์ธํ๋ผ (Equal ๊ธฐ์ค).
type cases = [
Expect<Equal<Includes<['Kars', 'Esidisi', 'Wamuu', 'Santana'], 'Kars'>, true>>,
Expect<Equal<Includes<['Kars', 'Esidisi', 'Wamuu', 'Santana'], 'Dio'>, false>>,
Expect<Equal<Includes<[1, 2, 3, 5, 6, 7], 7>, true>>,
Expect<Equal<Includes<[1, 2, 3, 5, 6, 7], 4>, false>>,
Expect<Equal<Includes<[1, 2, 3], 2>, true>>,
Expect<Equal<Includes<[1, 2, 3], 1>, true>>,
Expect<Equal<Includes<[{}], { a: 'A' }>, false>>,
Expect<Equal<Includes<[boolean, 2, 3, 5, 6, 7], false>, false>>,
Expect<Equal<Includes<[true, 2, 3, 5, 6, 7], boolean>, false>>,
Expect<Equal<Includes<[false, 2, 3, 5, 6, 7], false>, true>>,
Expect<Equal<Includes<[{ a: 'A' }], { readonly a: 'A' }>, false>>,
Expect<Equal<Includes<[{ readonly a: 'A' }], { a: 'A' }>, false>>,
Expect<Equal<Includes<[1], 1 | 2>, false>>,
Expect<Equal<Includes<[1 | 2], 1>, false>>,
Expect<Equal<Includes<[null], undefined>, false>>,
Expect<Equal<Includes<[undefined], null>, false>>,
]
type Includes<T extends readonly unknown[], U> = T extends [infer First, ...infer Rest]
? Equal<First, U> extends true
? true
: Includes<Rest, U>
: false;
- ์ฌ๊ท๋ก ๋ฐฐ์ด ํ์
- Equal์ ์ฌ์ฉํด ํ์ ๋๋ฑ์ฑ ๋น๊ต
โ 6. 3312 - Parameters
๋ฌธ์ : ํจ์ ํ์ ์ ํ๋ผ๋ฏธํฐ ํํ์ ์ถ์ถํ๋ผ (Parameters ์ง์ ๊ตฌํ).
function foo(arg1: string, arg2: number): void {}
function bar(arg1: boolean, arg2: { a: 'A' }): void {}
function baz(): void {}
type cases = [
Expect<Equal<MyParameters<typeof foo>, [string, number]>>,
Expect<Equal<MyParameters<typeof bar>, [boolean, { a: 'A' }]>>,
Expect<Equal<MyParameters<typeof baz>, []>>,
]
type MyParameters<T extends (...args: any[]) => any>
= T extends (...args: infer P) => any
? P
: never;
- infer P๋ก ๋งค๊ฐ๋ณ์ ๋ชฉ๋ก ์ถ์ถ
- ํจ์ ํ์ ์ ์ฝ: T extends (...args: any[]) => any
๋ฐ์ํ