Skip to content
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

'in' does not remove undefined from indexed value (with noUncheckedIndexedAccess) #61389

Open
Sainan opened this issue Mar 10, 2025 · 2 comments Β· May be fixed by #61393
Open

'in' does not remove undefined from indexed value (with noUncheckedIndexedAccess) #61389

Sainan opened this issue Mar 10, 2025 · 2 comments Β· May be fixed by #61393
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone

Comments

@Sainan
Copy link

Sainan commented Mar 10, 2025

πŸ”Ž Search Terms

in operator, noUncheckedIndexedAccess

πŸ•— Version & Regression Information

Tested 5.8.2 and 5.9.0-dev.20250310

⏯ Playground Link

https://www.typescriptlang.org/play/?noUncheckedIndexedAccess=true&ts=5.8.2#code/MYewdgzgLgBAhgJwQLhmArgWwEYFMEDaAujALwwECMANDAEy0DMRA3AFCiSwCWAJgB6oMOfGXrtuAMxgAKPvxjcw8JAEoYAbzYwdMTtBiC0WPAjGJC81tt36QAG1wA6eyADmM-qvYBfNkA

πŸ’» Code

With an array:

const arr: number[] = [1, 2, 3];
const idx: number = 2;
if (idx in arr) {
    const x: number = arr[idx];
    console.log(x);
}

With a map/object:

const map: Record<string, number> = { a: 1 };
const key: string = "a";
if (key in map) {
    const x: number = map[key];
    console.log(x);
}

πŸ™ Actual behavior

Type 'number | undefined' is not assignable to type 'number'.
  Type 'undefined' is not assignable to type 'number'.

πŸ™‚ Expected behavior

Expected 'in' operator to guarantee that the result of indexing is not undefined in this branch.

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

Duplicate of #54241.

@Andarist
Copy link
Contributor

There is some inconsistency here since #57847 though: TS playground

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases labels Mar 10, 2025
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Help Wanted You can do this Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
4 participants