-
Notifications
You must be signed in to change notification settings - Fork 12.8k
null
gets accidentally eliminated when narrowing by undefined's equality
#57693
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
Simpler repro: function f1<T extends Record<string, any>, K extends keyof T>(x: T[K] | undefined) {
if (x === undefined) return;
x; // T[K] & ({} | null)
if (x === undefined) return;
x; // T[K] & {}
} |
In addition to the odd narrowing behavior demonstrated by the OP and the simplified repro above, we also have inconsistencies in type relationships involving intersections that strip function f2<T, K extends keyof T>(t: T[K], p1: Partial<T>[K] & {}, p2: Partial<T>[K] & ({} | null)) {
t = p1;
t = p2; // Unexpected error
} The issue here is missing normalization of intersections in certain cases. Specifically, we only normalize intersections with |
@ahejlsberg that feels like an answer to this comment, right? I mean, this extra observed problem would fix what has been described there. Do you plan to work on fixing this? Or should I try to extend my PR with a fix for this? |
Furthermore, using function f3<T, P extends Record<string, any>>(t: T[keyof T], p1: P[keyof P], p2: P[keyof P] & {}) {
t = p1; // Error as expected
t = p2; // Missing error
}
function f4<T, P extends Record<string, unknown>>(t: T[keyof T], p1: P[keyof P], p2: P[keyof P] & {}) {
t = p1; // Error as expected
t = p2; // Error as expected
} This has to do with the constraint of |
Given type parameters
I will be putting up a PR that fixes the issues demonstrated by |
I can see that but it also means that the |
Well, it's allowed only because of the oddity of |
It has been called an oddity but I'm not sure if I should interpret this as "odd but working as intended" or "odd and it could be fixed" π In general, I interpret your comments about this here as the latter but I'm not 100% sure. |
π Search Terms
null undefined narrow narrowing reduction equality narrowby
π Version & Regression Information
*it worked differently in 5.1-5.3 because those had a bug in them ( #57202 )
β― Playground Link
https://www.typescriptlang.org/play?ts=5.5.0-dev.20240308#code/C4TwDgpgBAggdiA8gIwFYQMbCgXigJUwHsAnAEwB4BnYEgSzgHMAaKAQwQD4BuAKFEhQAysDbBoeeEjSZgfXmUwAbNiWgAzAK5wsdInCgALNlUQB3OAAUSRSCVAUAKlAgAPcXDJVYCFOiycABS8UFBEMlgAXFCOzCFQYDZg0da2EPYgANIQIHEAlNGJtlB03gDWOUTqMfIM4iTqbBjQIqQQFEK4wqLinFAA3vFUEMAiYu2ZLu4QnuWV1UJBFSDRmawAbmxKmhDRQgDamQC6BVDrRHRkfAC+vLxuYKTYWjrAegZUIDqtaqlgFPFOm4PF5uuM4qFLFMQd5LKo3lsOpwIVBJsCZqDllVhHEgjQ2ntgG0kawimAqClWMtVnkBvEMPoaGctjsusZTBY-ulQIEyVQqTlaQB+BJJKj7ZZHKDRbSKdQMCBXO6hOjVQKbbYSHB4WUQeVwRW0tTATQkOB8UIAektUGAhlKLhINhIVCGRLUADphqMehBAssNiyIHl5FabXaHVRDERNEoyHAAOTYNjqdSyW2GaACaAcEDRsysMwQBPrHNKNRsMggKAYTMYCpkdiMNgMJl26BUNgAWyz9qY8VVUHVQdw2qguv1hqgxtN5vi1qgVE0ZEUcGASmrdGwS4wzUVrtC+M93rG4n9OUDmpDvFuQA
π» Code
π Actual behavior
one of the calls errorrs while the other one doesn't
π Expected behavior
i'd expect consistent behavior at both call sites
Additional information about the issue
originally reported by @diegohaz here: https://twitter.com/diegohaz/status/1765674095293747628
The text was updated successfully, but these errors were encountered: