Why does the generic type not narrow after the type guard in typescript v4.4.4? Is there a way to resolve this issue?
type Data = X | Y | Z
type G<T extends Data> = {
type: 'x' | 'y'
data: T
}
type X = {
name: string
}
type Y = {
age: number
}
type Z = {
address: string
}
const isX = (item: G<Data>): item is G<X> => item.type === 'x'
const throwOnX = (item: G<Data>): G<Exclude<Data, X>> => {
if (!isX(item)) return item // Item remains as G<Data> instead of G<Y | Z>
throw Error('is X')
}