-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Allow == null
to narrow unknown
to null | undefined
#32798
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
I'd be interested in submitting a PR for this, by the way. Looking at checker.ts, |
@ericyhwang I think looking at the changes associated with #26941 is a good place to start—that added a new code path for unknown and |
@ericyhwang Are you working on this? I think it's a relatively simple fix, but it would also be nice for you to get a contributor badge. Do you want to sketch out the solution here together and then you can open a PR? |
@jack-williams Ah, I thought bugs needed a help-wanted label first. Haven't started beyond initially looking at the code. I'm still interested, though I may not have work-week time for a few weeks. If it's pretty simple, I might treat it like a mini weekend project and do it on my own time. |
Oh yeah, forgot about that label. Well I'm sure @RyanCavanaugh would be happy to accept a PR on this given that it's on the backlog. I'll leave it in your hands, but if you have any Q's just drop them in this issue. |
hi everyone, I faced this same compilation error TS2322 (without strictNullChecks) Is it the same problem discussing in this thread? Compilation error:
line 10:
However, I saw from the AST viewer, both computed type of |
…guard Co-authored-by: Hossein <hahmadia@users.noreply.github.com>
…guard Co-authored-by: Hossein <hahmadia@users.noreply.github.com>
TypeScript Version: typescript@3.6.0-dev.20190809
Search Terms: unknown, type guard, double-equals, ==, null
Code
Minimal repro case:
A more practical use-case for the narrowing would be something like this:
Expected behavior:
v == null
narrows anunknown
type tonull | undefined
, same asv === null || v === undefined
does.Reading the ECMAScript spec on
==
, I believe that== null
will guarantee that the value is either null or undefined.== undefined
would do the same thing, though linters generally prefer the shorter== null
.Actual behavior:
Compile error:
Playground Link: https://www.typescriptlang.org/play/#code/KYDwDg9gTgLgBAMwK4DsDGMCWEVxsAZxgAoA3ALjlQGsUIB3FASjgG8AoOOAG2HhSTduAeSgBVFABNgCTCmCTKAoXAA+VKTLkKA3JziYEcMnAC85uMu5r1pMxdTTZ8ySw5cuAek9wAKgAtMAjh6aGoCADp9LitRCSdtSTM4Uj0uAF99Q2M7Cys3aLhvPwBPMGA4AHIaOkZKg2C6eABDAgJMAHMUZoAjXjwIPDKKyqs1DQSXSqiPS0ERcU1nBWTU-UzMoA
Related Issues:
unknown
was added in Fix #25172: Add narrowing forunknown
with triple equals #26941== null
is well-defined and can be special-cased.unknown
#25720 open for better narrowing ofunknown
.The text was updated successfully, but these errors were encountered: