Is TypeScript failing to properly infer that when we are in the third case, item.id is not null? Is my understanding flawed, or is there a bug or edge case in TypeScript?
export const Type = [
"TypeA",
"TypeB",
"TypeC"
] as const
export type Type = typeof Type[number]
type Item = {
id: string | null,
type: Type
}
function stringFunction(some_string: string) {
console.log(some_string)
}
function test(item: Item) {
if (!item.id && item.type === Type[2]) return
switch (item.type) {
case 'TypeA':
break;
case 'TypeB':
break;
case 'TypeC':
// By uncommenting the following line, the type is correctly inferred in the third case
// But why is it needed ? A case analysis should suffice to know we have a non null item.id
// if (!item.id ) return
stringFunction(item.id)
break;
}
}