Open9

[Scrap] TypeScript の type-challenges をやってみる

ヘブンヘブン

type-challenges

型アサーションを解くクイズのようなもの。
https://github.com/type-challenges/type-challenges

日本語の README

https://github.com/type-challenges/type-challenges/blob/main/questions/00013-warm-hello-world/README.ja.md

ルール

  • 最後に取り組んだ問題を Pin する

参考

@type-challenges/utils

https://github.com/type-challenges/type-challenges/blob/main/utils/index.d.ts

Creating Types from Types

https://www.typescriptlang.org/docs/handbook/2/types-from-types.html

ヘブンヘブン

Hello World(お試し問題)

https://github.com/type-challenges/type-challenges/blob/main/questions/00013-warm-hello-world/README.ja.md

感想

内容としては簡単。ただファイルで import している @type-challenges/utilが気になった。
これを理解するだけでも勉強になりそう。気になった時にその都度参照できるようにしておきたい。
https://github.com/type-challenges/type-challenges/blob/main/utils/index.d.ts

ヘブンヘブン

Omit

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

感想

Pick の次にやるのはちょうど良かった気がする。以下のドキュメントを参考にした
https://www.typescriptlang.org/docs/handbook/2/mapped-types.html#key-remapping-via-as

型の評価の法則が若干わからん。
例えば以下だと、as Exclude が key と T のいずれに作用するのかよく分からなかった。結論は key の方。

type MyOmit<T, K extends keyof T> = {
  [key in keyof T as Exclude<key, K>]: T[key]
}