-
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 | undefined not assignable to type regardless of null check from Record with noUncheckedIndexedAccess #51582
Comments
For performance/correctness reasons we don't narrow when the key is a variable (we can't tell if it's been mutated or not). This limitation was called out in the PR text: #39560. |
Workaround: Assign |
So in those cases best pratice is something like first assign to a variable and checking the variable against null const arr: string[] = []
function addDefinitions(defs: Record<string, string>): void {
Object.keys(defs).forEach((type: string): void => {;
const def: string | undefined = defs[type] // <<< assign to const
if (def != undefined) {
arr.push(def)
}
});
} Or is there any better solution for this? |
I just found out, that i can use const arr: string[] = []
function addDefinitions(defs: Record<string, string>): void {
Object.entries(defs).forEach(([, def]): void => {;
arr.push(def)
});
} thanks you all for the quick help |
Bug Report
🔎 Search Terms
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
In my code i get a
Argument of type 'Definition | undefined' is not assignable to parameter of type 'Definition'.
error. But as you see i check the object value withif (defs[type] != undefined)
. But atthis.addDefinition(type, defs[type]);
the error is thrown anyway.The active option
noUncheckedIndexedAccess
will produces this error. But as you see there is a null check so that this error should not be thrown.🙂 Expected behavior
Because of the null check this error should not be shown.
The text was updated successfully, but these errors were encountered: