-
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
Non-null type assertions shouldn't affect optional chaining #34875
Comments
Another case. // b must be `ChildNode | 0` but actualy `ChildNode`
const b = document.querySelector('_')?.firstChild! ?? 0; |
Btw,
|
Memes aside, if the goal here is simply to remove the null possibility for |
This is the same as if you had written const _a = document.querySelector('_')?.textContent;
const a = _a!; IOW |
Can't you notice the problem that there is no way to infer the actual type |
Optional chaining covers common use cases with terse expressions. If you really need to go deep detail about each part of your expression; if it's important for your use case to distinguish when |
@falsandtru you'll have to get a time machine and take it up with TC39 in 2017, I guess. |
@RyanCavanaugh Then I just replace The question is TypeScript won't resolve the new problem that optional chaining sometimes makes type inference to be impossible to infer the actual type? |
cc @ahejlsberg
TypeScript Version: 3.7.x-dev.20191101
Search Terms:
Code
Expected behavior:
Type of a is string | undefined.
Actual behavior:
Type of a is string.
This means
document.querySelector('_').textContent!
is valid and these are the equivalent todocument.querySelector('_')!.textContent!
.Playground Link: http://www.typescriptlang.org/play/?ts=3.8.0-dev.20191101&ssl=1&ssc=53&pln=2&pc=23#code/MYewdgzgLgBAhjAvDAJiYBXAtgUzFAOgEcMcAnATwGUcAbHYKEMgCgHIB9NgSgH4CoOAB5QAwuEH4AhAG4gA
Related Issues:
The text was updated successfully, but these errors were encountered: