Why is TypeScript showing an error in the code below?
type A = { kind: "a" }
type B = { kind: "b" }
const a = (a: A): void => undefined
const b = (b: B): void => undefined
const c = <C extends A | B>(c: C): void => (c.kind == "a" ? a(c) : b(c))
It appears that TypeScript is having trouble determining the type of c
after checking c.kind == "a"
. Why is this happening?
The following variation seems to resolve the issue.
type A = { kind: "a" }
type B = { kind: "b" }
type C = A | B
const a = (a: A): void => undefined
const b = (b: B): void => undefined
const c = (c: C): void => (c.kind == "a" ? a(c) : b(c))