-
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
Undefined isn't always removed with check and noUncheckedIndexedAccess. #53454
Comments
Duplicate of #10530. Type narrowing does not occur for indexed access forms
Without this the type of |
Yeah it looks to be same cause, but that issue's original code has been fixed, whereas this is still happening in TypeScript 5.0.2, so it's not quite a duplicate. The fix to move it into a local It would also be nice if TypeScript typing information actually showed that it was a literal, even though it says a more generic |
Yeah, it's kinda unfortunate, that issue was a catch all for indexed access narrowing. Numerous issues reporting the exact issue as yours have been closed as a duplicate of that one. |
Yeah, I agree. The last comment mentioned splitting some of the other cases up. I figure this is a super simple example and so might be helpful to keep this one open. @RyanCavanaugh this isn't a duplicate of that issue, but if you have another issue that this is a duplicate of, then we could refer to that one, since it does seem that there are other cases that exist. It seems like the #10530 issue should be closed in deference to others since it already has a solution with a merged PR. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
π Search Terms
noUncheckedIndexedAccess
π Version & Regression Information
I don't know when this started, but I'm using TypeScript 5.0.2.
β― Playground Link
Playground Link
π» Code
Using TypeScript 5.0.2 with
noUncheckedIndexedAccess
set to true.π Actual behavior
The type of
b
isstring | undefined
, when it should be juststring
.However, if you remove the
+ ''
in thex = ...
line, thenb
correctly showsstring
.π Expected behavior
I would expect
b
to always be reduced tostring
becausea[x]
is checked for undefined and assigned beforehand.It seems odd that adding
+ ''
, or anything else really, seems to remove the ability to reduceb
to a string in the subsequent commands, even thoughx
is astring
in both cases. It seems like there is some hidden typing information not shown.The text was updated successfully, but these errors were encountered: