Skip to content

Type guard is not working with noUncheckedIndexedAccess and destructuring #52623

Closed

Description

@PunGy

Bug Report

When noUncheckedIndexedAccess is on and you try to use a type guard like typeof list[index] !== undefined, it would not work if index was created via destructuring assignment or from the tuple.

🔎 Search Terms

noUncheckedIndexedAccess index destructuring undefined tuple

🕗 Version & Regression Information

  • Compiler error
  • Tested in 4.1.5, 4.5.5, and 4.9.5.

⏯ Playground Link

Playground link

💻 Code

// index is a number
const index = 1
// indexD is a number
const [indexD] = [1]

const tuple: [number] = [1]
const indexT = tuple[0]

const list: Array<number> = [1, 2, 3]

// Block A
if (typeof list[index] === 'number') {
  // `num` has a `number` type
  const num = list[index]
}
// Block B
if (typeof list[indexD] === 'number') {
  // `num` has a `number | undefined` type
  const num = list[indexD]
}
// Block C
if (typeof list[indexT] === 'number') {
  // `num` has a `number | undefined` type
  const num = list[indexT]
}

🙁 Actual behavior

Type guard does not work with the num in the Block B and C

🙂 Expected behavior

num in the Block B and C should have the same type as the num in the Block A

Activity

  1. changed the title [-]Type guards are not working with `noUncheckedIndexedAccess` and destructuring[/-] [+]Type guard is not working with `noUncheckedIndexedAccess` and destructuring[/+] on Feb 5, 2023
  2. MartinJohns commented on Feb 5, 2023

    @MartinJohns
    Contributor

    Duplicate of #10530. Type narrowing does not occur for indexed access forms e[k] where k is not a literal.

  3. fatcerberus commented on Feb 5, 2023

    @fatcerberus

    Workaround: Assign list[index] to a const first then type-guard that.

  4. PunGy commented on Feb 6, 2023

    @PunGy
    Author

    Duplicate of #10530. Type narrowing does not occur for indexed access forms e[k] where k is not a literal.

    I see... But still quite strange that even when you declare something as a constant literal, for example:

    const [indexT] = [1] as const // here indexT has a type '1', and still doesn't work
    const index: number = 1 // index has a 'number' type, not literal 1
    
    if (typeof list[indexT] === 'number') num = list[indexT] // number | undefined
    if (typeof list[index] === 'number') num = list[index] // number

    Very confusing behavior

  5. typescript-bot commented on Feb 16, 2023

    @typescript-bot
    Contributor

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

  6. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions