NISHIO Hirokazu[English][日本語]

Examples of generics needed

Minimal code to reproduce the problem ts

type X = "A" | "B";
const f = (x: X): X => (x);
const a: "A" = f("A");  // error

error

  • Type 'X' is not assignable to type '"A"'.
    • Type '"B"' is not assignable to type '"A"'

solution ts

const f = <T extends X>(x: T): T => (x);
const a: "A" = f("A");  // ok

This page is auto-translated from /nishio/ジェネリクスが必要な例 using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I'm very happy to spread my thought to non-Japanese readers.


(C)NISHIO Hirokazu / Converted from Markdown (en)
Source: [GitHub] / [Scrapbox]