type-challengesにリベンジした

この前参加したtypescriptの勉強会で、type-challengesが紹介されてた。

2,3年前にやったときは1問も解けなかった悔しい思い出が蘇ってきたので、今ならもう少し解けるかも…!?と思ってリベンジした 💪

easy

Pick ✅

type MyPick<T, K extends keyof T> = {
  [k in K]: T[k]
}

https://github.com/type-challenges/type-challenges/blob/main/questions/00004-easy-pick/README.md

Readonly ✅

type MyReadonly<T> = {
  readonly [k in keyof T]: T[k]
}

https://github.com/type-challenges/type-challenges/blob/main/questions/00007-easy-readonly/README.md

Tuple To Object ✅

type TupleToObject<T extends readonly any[]> = {
  [k in T[number]]: k
}

https://github.com/type-challenges/type-challenges/blob/main/questions/00011-easy-tuple-to-object/README.md

First ✅

type First<T extends any[]> = T extends [] ? never : T[0]

https://github.com/type-challenges/type-challenges/blob/main/questions/00014-easy-first/README.md

Tuple Length ❌

うーん、、簡単なのに思い付かず解けなかった…😇

https://github.com/type-challenges/type-challenges/blob/main/questions/00018-easy-tuple-length/README.md

Exclude ✅

type MyExclude<T, U> = T extends U ? never : T

https://github.com/type-challenges/type-challenges/blob/main/questions/00043-easy-exclude/README.md

Awaited ❌

これは惜しかった…Promise使っちゃってたけどPromiseLikeを使わなければならなかった。。

type MyAwaited<T> = T extends Promise<infer P> ? P extends Promise<any> ? MyAwaited<P> : P : never

https://github.com/type-challenges/type-challenges/blob/main/questions/00189-easy-awaited/README.md

If ✅

type If<C, T, F> = C extends true ? T : F

https://github.com/type-challenges/type-challenges/blob/main/questions/00268-easy-if/README.md

Concat ✅

type Concat<T extends any[], U extends any[]> = [...T, ...U]

https://github.com/type-challenges/type-challenges/blob/main/questions/00533-easy-concat/README.md

Includes ❌

配列から1つずつ取れば良いのねーー、再帰的な発想がでなくて困った…

https://github.com/type-challenges/type-challenges/blob/main/questions/00898-easy-includes/README.md

Push ✅

type Push<T extends any[], U> = [...T, U]

https://github.com/type-challenges/type-challenges/blob/main/questions/03057-easy-push/README.md

Unshift ✅

type Unshift<T extends any[], U> = [U, ...T]

https://github.com/type-challenges/type-challenges/blob/main/questions/03060-easy-unshift/README.md

Parameters ✅

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

https://github.com/type-challenges/type-challenges/blob/main/questions/03312-easy-parameters/README.md

medium

Get Return Type ✅

type MyReturnType<T> = T extends (...args: any[]) => infer P ? P : never

https://github.com/type-challenges/type-challenges/blob/main/questions/00002-medium-return-type/README.md

Omit ❌

うわーー、ここでas使えたんか

https://github.com/type-challenges/type-challenges/blob/main/questions/00003-medium-omit/README.md

Readonly 2 ✅

type MyReadonly2<T, K extends keyof T = keyof T> = {
  readonly[k in K]: T[k]
} & {
  [k in keyof T as k extends K ? never : k]: T[k]
}

https://github.com/type-challenges/type-challenges/blob/main/questions/00008-medium-readonly-2/README.md

Deep Readonly ❌

無理だった。tupleの扱い方の理解が足りなかったのと、keyof A extends neverの理解が足りてなかった😫

type DeepReadonly<T> = {
  readonly [k in keyof T]: T[k] extends {}|any[] ? DeepReadonly<T[k]> : T[k]
}

https://github.com/type-challenges/type-challenges/blob/main/questions/00009-medium-deep-readonly/README.md

Tuple to Union ✅

type TupleToUnion<T extends any[]|any> = T extends [infer Left, ...infer Right] ? Left|TupleToUnion<Right> : never

https://github.com/type-challenges/type-challenges/blob/main/questions/00010-medium-tuple-to-union/README.md

Chainable Options ✅

type Chainable<X = {}> = {
  option<T, U extends string>(key: U, value: T):Chainable<{[k in U]: T} & Omit<X, U>>
  get(): X
}

https://github.com/type-challenges/type-challenges/blob/main/questions/00012-medium-chainable-options/README.md

ここら辺で体力無くなったので終わった。

感想

  • easyは解けた!(2問落としましたが
  • mediumの前半から徐々にわからないところが増えてきた、、時間かければ解けなくはなさそうな感じ
  • mediumの後半は多分できなさそう
  • 問題によっては瞬足で終わるものもあって、相性良い悪い問題があるんやろなと思った
  • 2,3年前の自分と比べたら圧倒的に理解が深まってる…!

See also