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

Type guard is not working with noUncheckedIndexedAccess and destructuring #52623

Closed
PunGy opened this issue Feb 5, 2023 · 4 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@PunGy
Copy link

PunGy commented Feb 5, 2023

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

@PunGy PunGy changed the title Type guards are not working with noUncheckedIndexedAccess and destructuring Type guard is not working with noUncheckedIndexedAccess and destructuring Feb 5, 2023
@MartinJohns
Copy link
Contributor

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

@fatcerberus
Copy link

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

@PunGy
Copy link
Author

PunGy commented Feb 6, 2023

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

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 13, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' 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
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants