Closed as not planned
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