-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Poor narrowing of contextual unions that don't share properties #48460
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
Comments
This is fully intentional because object types aren't sealed - it's perfectly legal to have const m = {
a: true,
b: true,
cb: (x: string) => null
};
const s: DiscriminatorA = m; Whether type DiscriminatorA = {
a: true;
b?: never;
cb: (x: string) => void;
}
type DiscriminatorB = {
a?: never;
b: true;
cb: (x: number) => void;
} |
I understand they're not sealed, but as written, the object literal
is not assignable to |
It could be made to work for that case, but raises other questions like what to do with this f({
a: true,
b: true,
cb: n => n // string | number? string & number?
}); Overall I think your assessment of trade-offs is correct -- this pattern doesn't really work outside this immediate example and isn't worth spending cycles on |
In the example above, the current behavior seems correct, you can't narrow the type in the union. Whether you would then infer something about the unknown property seems like a different issue. There are some potential cases where this comes up due to not having a discriminable member e.g. this playground, and more so came up in picking optional properties in the same context (#48448). But given your assessment, I'm cool with closing until/if there's an actual use-case that justifies it. |
Bug Report
In cases with discriminant unions that don't fit the conventional form (e.g. one property that's constant among all union members) type discrimination doesn't happen as one might expect or desire, see the code example.
I tried to search to see if this had been talked about before, but couldn't find anything. I imagine the current design was around capturing a common pattern in js while not spending too much time trying to narrow the types.
To address this in the same framework:
discriminateTypeByDiscriminableItems
andisDiscriminantProperty
would have to be tweaked to handle missing properties and it's not clear how that would work withexactOptionalPropertyTypes
. Alternatively, the existing code may work by first filtering the union down to feasible assignments based on the exitance of properties, e.g. only consider types that the object literal has every non-optional property. However, doing that correctly would require constructing a filtered union type and I'm not sure if there's precedent for that.Doing any of this will likely cause a performance penalty, so I think it's also fair to say this pattern is infrequent enough to not care about getting correct.
🔎 Search Terms
discriminant unions narrowing
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Parameter 'n' implicitly has an 'any' type.(7006)
🙂 Expected behavior
type checks successfully
The text was updated successfully, but these errors were encountered: