TypeScript Type Challenges
https://github.com/type-challenges/type-challenges
Warm up
Hello World
stringに変えて問題ないかの確認
https://github.com/type-challenges/type-challenges/blob/main/questions/00013-warm-hello-world/README.md
Medium
Get Return Type
https://github.com/type-challenges/type-challenges/blob/main/questions/00002-medium-return-type/README.md
code:typescript
type MyReturnType<T extends (...args: any[]) => any> = T extends (...args: any[]) => infer R ? R : never
Answer
Chainable Options
https://github.com/type-challenges/type-challenges/blob/main/questions/00012-medium-chainable-options/README.md
上書き
除外ルールが大分手間
上書きできるけど、同じtypeの上書きはNG
wintyo.icon 型的には不要だけど、値は変更されているからNGにするの実際は変じゃない?
code:typescript
type Chainable<T = {}> = {
option<K extends string, V>(
key: K extends keyof T
? V extends TK
? never
: K
: K,
value: V
): Chainable<Omit<T, K> & { key in K: V }>
get(): T
}
Answer
Promise.all
https://github.com/type-challenges/type-challenges/blob/main/questions/00020-medium-promise-all/README.md
可変長引数として受け取ることでタプルになる
先にreadonlyを宣言させることでそっちで吸収される
code:typescript
declare function PromiseAll<T extends any[]>(values: readonly ...T): Promise<{
K in keyof T: TK extends Promise<infer U> ? U : TK
}>
Answer
Type Lookup
https://github.com/type-challenges/type-challenges/blob/main/questions/00062-medium-type-lookup/README.md
code:typescript
type LookUp<U, T> = U extends { type: T } ? U : never
Answer
Append Argument
https://github.com/type-challenges/type-challenges/blob/main/questions/00191-medium-append-argument/README.md
引数に追加させる
wintyo.icon inferで拾ったparametersから一つ加えるだけでいけそう
Flip Arguments
https://github.com/type-challenges/type-challenges/blob/main/questions/03196-medium-flip-arguments/README.md
BEM style string
https://github.com/type-challenges/type-challenges/blob/main/questions/03326-medium-bem-style-string/README.md
Inorder Traversal
https://github.com/type-challenges/type-challenges/blob/main/questions/03376-medium-inordertraversal/README.md
wintyo.icon 二分探索木でアクセスする順に拾ってこいってこと?
Flip
https://github.com/type-challenges/type-challenges/blob/main/questions/04179-medium-flip/README.md
Zip
https://github.com/type-challenges/type-challenges/blob/main/questions/04471-medium-zip/README.md
IndexOf
https://github.com/type-challenges/type-challenges/blob/main/questions/05153-medium-indexof/README.md
wintyo.icon TSでやることじゃない。。
code:typescript
type StrictEqual<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false;
type IndexOf<T, U, Arr extends any[] = []> = T extends infer X, ...infer R
? StrictEqual<X, U> extends true
? Arr'length'
: IndexOf<R, U, ...Arr, X>
: -1
Answer
LastIndexOf
https://github.com/type-challenges/type-challenges/blob/main/questions/05317-medium-lastindexof/README.md
code:typescript
type StrictEqual<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false;
type LastIndexOf<T, U> = T extends ...infer R, infer X
? StrictEqual<X, U> extends true
? R'length'
: LastIndexOf<R, U>
: -1
Answer
Unique
https://github.com/type-challenges/type-challenges/blob/main/questions/05360-medium-unique/README.md
MapTypes
https://github.com/type-challenges/type-challenges/blob/main/questions/05821-medium-maptypes/README.md
Construct Tuple
https://github.com/type-challenges/type-challenges/blob/main/questions/07544-medium-construct-tuple/README.md
#TypeScript