NISHIO Hirokazu[Translate]
ジェネリクスが必要な例
問題を再現する最小限のコード
ts
type X = "A" | "B"; const f = (x: X): X => (x); const a: "A" = f("A"); // error

エラー
Type 'X' is not assignable to type '"A"'.
Type '"B"' is not assignable to type '"A"'

解決方法
ts
const f = <T extends X>(x: T): T => (x); const a: "A" = f("A"); // ok


"Engineer's way of creating knowledge" the English version of my book is now available on [Engineer's way of creating knowledge]

(C)NISHIO Hirokazu / Converted from [Scrapbox] at [Edit]