-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type narrowing is not propagated to a nested discriminated union #57218
Comments
This looks similar to #53202 and #18758, but I'm not sure. Workaround: export type FixedAnimalWithOffspring = {
[S in Species]: AnimalWithOffspring<S>
}[Species];
let anotherAnimal = {} as FixedAnimalWithOffspring;
if (anotherAnimal.species === Species.Dog) {
anotherAnimal.bork(); // OK
anotherAnimal.offspring.bork(); // OK
} |
I mean, given the types as written, itβs entirely possible for |
@fatcerberus Could you explain how is it possible that |
Resolving type aliases for clarity, you started with a |
Okay, fair enough, but isn't it weird though? type AnimalWithOffspring<S extends Species> = AnimalOfType<S> & { offspring: AnimalOfType<S> }; it makes me naturally think that S is going to be the same between two subtypes. And thus my expectation is that when it is narrowed for one subtype, it should be narrowed for another (since it's the same S). |
Intuition suggests the types must correlate between // Look ma! No errors!
let someAnimal = {
species: Species.Dog,
bork: () => 'woof!',
offspring: {
species: Species.Cat
}
} satisfies AnimalWithOffspring<Species>; |
I get it for when let someAnimal = {
species: Species.Dog,
bork: () => 'woof!',
offspring: {
species: Species.Cat
}
} satisfies AnimalWithOffspring<Species.Dog>; correctly produces an error. |
Right, but you don't have the variable defined as Rather, your condition can only narrow it to It's confusing, but it's not incorrect. |
@undsoft Simplifying this as far as possible, basically what you have is this: type Fooey<T> = { a: T, b: T };
type Foo = Fooey<string | number>; This gives you You have to think of type aliases as type-functions - once you've invoked Footnotes
|
No amount of fixes are going to change the fact that |
This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
type narrowing, discriminated union, type narrowing propagation, nested type narrowing
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?ts=5.3.3&ssl=36&ssc=2&pln=27&pc=14#code/KYOwrgtgBAygDsAxgS2AZygbwFBSgEQHsBzAGlygGEBDAF2wF9ttkRbgAnAM2sWAJJYKaBCnQAuWKNRoAdEWIBuCgCNCHANaSAFAEooAXgB8UAG6FkAE2VMWbTjz5U6QvCKQzJ8D+lk1aNsy0AJ4IUACCIMgQ1AA2hs60UAA+AkpBofyR0XEA8lwAKpkAPDBQwAAe7CCWGN5iaCYGUACiVRy8tMXZMbGkWFDuDV5QDEbK2CFhPXEA6si0ABb5XCIcrMSl5VWgtVI+jQkzsflFCKUmAGQDhFyrcOsgxJLHpyUwJgwTscBJaIQQYDHBKYBhQagYY7zJYrNYbUrSdDjFhcKDaf6A46yIYyQwGZr1GTyEj6HBuAFAqK9WRqTR6RRQAD0jKguQA0sIKVjbvdHsQaeoNPSmSyAAocQgIDghKAAclpGllUEshHQUBAhCSlWQaCShBAUCm-Flr0K70RjVljGYzKgs0F1AlYBq2EqcHUSSNUAAYsgKsBLFCFss7nCniCKABtMqsfYNAC6Lypc2DsIe8I+jGjFvj31+4I1S04wOaoPBGF9-sDydi0JDvI2ymQqO01ELi2LNexFrxBItxOIpIobc1HY4WIVwtt7OH7c7OVish5Yf5k90DOnHIYQA
π» Code
π Actual behavior
I get an error of
Property 'bork' does not exist on type 'AnimalOfType<Species>'
because type narrowing did not propagate tooffspring
.π Expected behavior
Offspring type is narrowed to the same animal as parent.
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: