-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
Type guard is not working with noUncheckedIndexedAccess
and destructuring
#52623
Comments
noUncheckedIndexedAccess
and destructuringnoUncheckedIndexedAccess
and destructuring
Duplicate of #10530. Type narrowing does not occur for indexed access forms |
Workaround: Assign |
I see... But still quite strange that even when you declare something as a constant literal, for example: const [indexT] = [1] as const // here indexT has a type '1', and still doesn't work
const index: number = 1 // index has a 'number' type, not literal 1
if (typeof list[indexT] === 'number') num = list[indexT] // number | undefined
if (typeof list[index] === 'number') num = list[index] // number Very confusing behavior |
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
When
noUncheckedIndexedAccess
is on and you try to use a type guard liketypeof list[index] !== undefined
, it would not work ifindex
was created via destructuring assignment or from the tuple.🔎 Search Terms
noUncheckedIndexedAccess index destructuring undefined tuple
🕗 Version & Regression Information
⏯ Playground Link
Playground link
💻 Code
🙁 Actual behavior
Type guard does not work with the
num
in the Block B and C🙂 Expected behavior
num
in the Block B and C should have the same type as thenum
in the Block AThe text was updated successfully, but these errors were encountered: