Why in this code the narrowing doesn’t work within the switch case in the function but it works when calling the function.
export enum Kind {
A,
B,
C,
}
export interface KindMap {
[Kind.A]: {
a: string
}
[Kind.B]: {
b: number
}
[Kind.C]: {
c: string[]
}
}
const func = <T extends Kind>(kind: T, info: KindMap[T]) => {
switch (kind) {
case Kind.A: {
info.a // error, narrowing doesn't work here
break
}
}
}
// Narrowing works correctly here
func(Kind.A, { a: 'string' })
func(Kind.B, { b: 1 })
func(Kind.C, { c: ['string'] })
Playground