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

noUncheckedIndexedAccess does not work correctly with Array #45306

Closed
SuperITMan opened this issue Aug 3, 2021 · 2 comments
Closed

noUncheckedIndexedAccess does not work correctly with Array #45306

SuperITMan opened this issue Aug 3, 2021 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@SuperITMan
Copy link

SuperITMan commented Aug 3, 2021

Bug Report

πŸ”Ž Search Terms

noUncheckedIndexedAccess with arrays

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about noUncheckedIndexedAccess

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

// @strict: true
// @noUncheckedIndexedAccess: true

const arr: string[] = [];

const test = () => {
    if (arr.length > 1) {
        // As arr.length > 1, we expect that length >= 2. That means arr[0] should be a `string`
        const str: string = arr[arr.length - 2]; 
        console.log(str);
    }

    // Same here, we check arr.length == 1, so arr[0] should be a `string` and cannot be `undefined` according to declaration `const arr: string[] = [];`
    if (arr.length === 1 && arr[0].match(/test/)) { 
        console.log("First index matches 'test'");
    }
}

πŸ™ Actual behavior

Get following errors:

error TS2322: Type 'string | undefined' is not assignable to type 'string'.
  Type 'undefined' is not assignable to type 'string'.
    const str: string = this.arr[this.arr.length - 2];

error TS2532: Object is possibly 'undefined'.
    if (arr.length === 1 && arr[0].match(/test/))

πŸ™‚ Expected behavior

The code should not generate any error since the array is type string[] and not (string|undefined)[]

SuperITMan added a commit to SuperITMan/code-style that referenced this issue Aug 3, 2021
Add new TypeScript property `"noUncheckedIndexedAccess": false`.
This new property should not be enabled as it returns false positive with array.
See: microsoft/TypeScript#45306
SuperITMan added a commit to SuperITMan/code-style that referenced this issue Aug 3, 2021
Add new TypeScript property `"noUncheckedIndexedAccess": false`.
This new property should not be enabled as it returns false positive with array.
See: microsoft/TypeScript#45306
@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Aug 4, 2021
@RyanCavanaugh
Copy link
Member

This was one of the outlined caveats of the flag; see #13778

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants