Here is code:
type T = 'a' | "b"
type M = {
a: 1,
b: 2
}
function a(a: 'a') {}
function m1(a: 1) {}
function f<TT extends T>(t: TT, p: M[TT]) {
switch (t) {
case "a": {
a(t)
m1(p) // Argument of type '1 | 2' is not assignable to parameter of type '1'
break
}
}
}
Why p wasn't narrowed to 1? Are there a neat workaround without type casts?
TypeScript is unable to recognize that
pcorrolates withtas explained in microsoft/TypeScript#35873. You'll need another type guard to narrowp. The most straightforward syntax is to useswitch (true)narrowing which was recently added in TypeScript 5.3.TypeScript Playground