-
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
Narrowing on assignment does not remove weak types #26405
Comments
@ahejlsberg tentatively tagging Bug but we'll figure out what we really want to do |
Continuing the work toward a solution from #26143 (comment): I tried:
So I think it's most practical to just fix weak types. To recap, we want to (1) decompose unions on the source side, (2) consider the constraints of indexed access types on the source side and decompose unions again after doing so, and (3) check for weak types. The old implementation did (1) and (3) but not (2); the one from #26143 does (1) and (2) but not (3). ISTM it's going to be awkward to get (2) without building on the existing |
types. Fixes microsoft#26405. Also add a debug assert to catch any future cases like microsoft#26130 in which the target type of a valid assignment is narrowed to something to which the source type is no longer assignable.
Proposed fix is at #26425. |
…microsoft#26130 by skipping narrowing if the old algorithm produces a type to which the assigned type is not assignable. This also means we'll no longer narrow for erroneous assignments where the assigned type is not assignable to the declared type. This is the reason for the numericLiteralTypes3 baseline change. Fixes microsoft#26405.
Wait, I have another idea: use the old algorithm but if it messes up and produces a narrowed type to which the assigned type is not assignable (as in #26130), then skip narrowing and use the declared type. I updated #26425. By the way, another example that confuses the old algorithm (playground): // Enable strictNullChecks
function test<T extends number | string>(arg: null | T) {
let x: null | number | string = arg;
if (x == null)
return;
x; // never
} |
Filing an issue to track the regression from #26143 described in #26143 (comment).
TypeScript Version: master (fe387cc)
Search Terms: narrow assignment weak type assignable comparable
Code
Expected behavior: No error.
Actual behavior: Error as indicated above.
Playground Link: N/A, playground is not affected
Related Issues: #26143
The text was updated successfully, but these errors were encountered: