-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed as not planned
Closed as not planned
Copy link
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
Description
π Search Terms
type narrowing, discriminated union, type narrowing propagation, nested type narrowing
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ.
β― Playground Link
π» Code
enum Species {
Dog,
Cat
}
interface Dog {
species: Species.Dog;
bork: () => void;
}
interface Cat {
species: Species.Cat;
}
type Animal = Cat | Dog;
type AnimalOfType<S extends Species> = Extract<Animal, { species: S }>;
type AnimalWithOffspring<S extends Species> = AnimalOfType<S> & { offspring: AnimalOfType<S> };
let someAnimal = {} as AnimalWithOffspring<Species>;
if (someAnimal.species === Species.Dog) {
someAnimal.bork(); // OK
someAnimal.offspring.bork(); // Property 'bork' does not exist on type 'AnimalOfType<Species>'
}π Actual behavior
I get an error of Property 'bork' does not exist on type 'AnimalOfType<Species>' because type narrowing did not propagate to offspring.
π Expected behavior
Offspring type is narrowed to the same animal as parent.
Additional information about the issue
No response
Metadata
Metadata
Assignees
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug