-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Unexpected error: TS2322: Type 'string | undefined' is not assignable to type 'string' #55206
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
It is adding the
The flag forces you to make sure you actually got a valid value when using indexed access. |
It reports errors in code unexpectedly. Filed a report to clarify what is the problem: microsoft/TypeScript#55206
@MartinJohns with that link, I can understand the purpose of the flag better. ty! Indeed, it seems helpful that it flags accesses such as What would be the ways to write code that accesses arrays? This is my thinking:
function testfunc(urls: string[]): void {
let i = 0;
for (const url of urls) {
const image = new Image();
image.src = url;
// Use 'i': Load the img to a GPU array texture with slice 'i'.
i++;
}
}
if (urls[i] !== undefined)
image.src = urls[i];
else
throw new Error('Should never happen');
Any better suggestions? As for action, does it make sense and is it possible to refine the option so it still allows indexing with numbers or take the range checking control flow into account? (again, not a TS expert here) I feel the option is triggering on code that is quite common and demonstrably safe. I either have to disable it, or it's leading me to change my code into making other kinds of errors (like typing a At a minimum, can the option documentation be updated to include the information from the release notes and guide people on how to write code that doesn't trigger an error? |
…which is exactly why the flag is disabled by default, even under |
The for (const [i, url] of urls.entries()) {
// Can use both the index and url here
} |
This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
So shouldn't this be a feature request instead of closed? |
Bug Report
🔎 Search Terms
TS2322 ; "undefined is not assignable to type" ; array iteration ; noUncheckedIndexedAccess
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
With
noUncheckedIndexedAccess=true
🙂 Expected behavior
urls
is declared as an array ofstring
, notstring | undefined
, therefore I expect the assignment inside the function to be safe and an error on the calling side of the function if someone tries to pass in an array with potentially undefined things in it.I don't understand why the for loop indexing makes it unsafe? Or how I should write my code then?
I have looked at the option documentation, at stack overflow and at the internet for far too long :))
I don't understand the documentation example (I don't know about 'unknown properties').
noUncheckedIndexedAccess
is incorrectly adding 'undefined' for this case?Another minimal example that I would not expect to cause an error:
Background context:
I'm not a JS or TS expert. I have recently started using TypeScript and porting a JS project to it. I incrementally enabled all error reporting options and strictness that I found :)
Obviously it must have been my error and messy enabling of options, but I'm pretty sure that everything worked at some point and then I noticed this error days later. It was quite time consuming to search for it and track it to the for loop and specifically to
noUncheckedIndexedAccess
.The text was updated successfully, but these errors were encountered: