Closed as not planned
Description
π Search Terms
noUncheckedIndexedAccess incorrect undefined
π Version & Regression Information
N/A
β― Playground Link
π» Code
declare const r: Record<string, string>;
function foo(key: string): string {
key = 'something'; // Comment out this, and the error disappears!
r[key] ??= 'bar';
return r[key]; // Error: Type 'string | undefined' is not assignable to type 'string'.
}
π Actual behavior
Error: Type 'string | undefined' is not assignable to type 'string'.
Incorrect because r[key]
cannot be undefined
.
π Expected behavior
No error, and that the behavior is the same whether or not the line key = 'something';
is commented out.
Additional information about the issue
Originally filed in typescript-eslint: typescript-eslint/typescript-eslint#11343