Open
Description
https://github.com/type-challenges/type-challenges/blob/main/README.md
4 - Pick
type MyPick<T, K extends keyof T> = {[x in K]: T[x]}
7 - Readonly
// type MyReadonly<T> = Readonly<T>
type MyReadonly<T> = {
readonly [x in keyof T]: T[x]
}
11 -Tuple to Object
type TupleToObject<T extends readonly any[]> = { [x in T[number]]: x}
14 - First of Array
type First<T extends any[]> = T extends [infer E, ...unknown[]] ? E : never
- never를 잘 몰랐군...
- https://www.zhenghao.io/posts/ts-never
- infer도 몰랐군...
- https://www.zhenghao.io/posts/type-programming#local-variable-declaration
18 - Length of Tuple
// type Length<T extends readonly any[]> =
// 여기까지만 하다가 length를 아는 법 서치하다가 알게 됨
type Length<T extends readonly any[]> = T['length']
- 실제 값들을 타입으로서 사용할 수 있는데 그 점을 자꾸 까먹는 것 같음 ㅎ
43 - Exclude 🤔
type MyExclude<T, U> = T extends U ? never : T;
189 - Awaited 🤔
tslib 참고
/**
* Recursively unwraps the "awaited type" of a type. Non-promise "thenables" should resolve to `never`. This emulates the behavior of `await`.
*/
type Awaited<T> =
T extends null | undefined ? T : // special case for `null | undefined` when not in `--strictNullChecks` mode
T extends object & { then(onfulfilled: infer F, ...args: infer _): any } ? // `await` only unwraps object types with a callable `then`. Non-object types are not unwrapped
F extends ((value: infer V, ...args: infer _) => any) ? // if the argument to `then` is callable, extracts the first argument
Awaited<V> : // recursively unwrap the value
never : // the argument to `then` was not callable
T; // non-object or non-thenable
268 - If
type If<C extends Boolean, T, F> = C extends true ? T : F
- C is true 같은 문법이 뭘까 생각했는데 extends 쓰면 되는 거였음
533 - Concat
type Concat<T extends any[], U extends any[]> = [...T, ...U]
- any와 unknown... 둘 다 가능한데 둘의 차이를 더 알아보자
898 - Includes
type Includes<T extends readonly any[], U> = U extends T[number] ? true : false
// 일부 케이스에서 error
3057 - Push
type Push<T extends any[], U> = [...T, U]
- concat과 비슷
3060 - Unshift
type Unshift<T extends any[], U> = [U, ...T]
3312 - Parameters
type MyParameters<T extends (...args: any[]) => any> = T extends (...args: infer A) => any ? A : never
- infer 써먹기...! extends를 좀 더 편하게 쓸 수 있게 되었슴