Closed
Description
Bug Report
π Search Terms
type narrowing, generics
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about type narrowing
β― Playground Link
π» Code
enum Enum {
A = 1,
B = 2
}
type EnumPair = {
[Enum.A]: {
x: number;
};
[Enum.B]: {
y: number;
};
}
const makePair = <T extends Enum>(first: T, second: EnumPair[T]) => {
if (first === Enum.A) {
console.log(second.x)
}
}
π Actual behavior
Property 'x' does not exist on type '{ x: number; } | { y: number; }'.
Property 'x' does not exist on type '{ y: number; }'.(2339)
π Expected behavior
I expected typescript to narrow the type of T to the specific enum being used (due to the equality operator), and therefore the enum pair too.
Metadata
Metadata
Assignees
Labels
No labels