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.