typescript v4.4.4 Why generic type is not narrowed after type guard? How do I fix it?
type Data = A | B | C
type G<T extends Data> = {
type: 'a' | 'b'
data: T
}
type A = {
foo: string
}
type B = {
bar: string
}
type C = {
foobar: string
}
const isA = (item: G<Data>): item is G<A> => item.type === 'a'
const throwOnA = (item: G<Data>): G<Exclude<Data, A>> => {
if (!isA(item)) return item // Item still G<Data> instead of G<B | C>
throw Error('is A')
}