Skip to content

noUncheckedIndexedAccess narrowing fails when utilizing a stored const string literal #42979

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

Closed
berickson1 opened this issue Feb 26, 2021 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@berickson1
Copy link

Bug Report

🔎 Search Terms

noUncheckedIndexedAccess
string literal constant

🕗 Version & Regression Information

Introduced with noUncheckedIndexedAccess - present in all Typescript versions 4.1+

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

⏯ Playground Link

Playground link with relevant code

💻 Code

const LOOKUP_KEY = 'test';

function logNumber(val: number){
  console.log(val);
}
function doStuff(input: {[key: string]: number}) {
  if (input[LOOKUP_KEY]) {
    logNumber(input[LOOKUP_KEY]); // Argument of type 'number | undefined' is not assignable to parameter of type 'number'
  }
}
function doStuff2(input: {[key: string]: number}) {
  if (input['test']) {
    logNumber(input['test']); // Works as expected
  }
}

function doStuffScopedConstant(input: {[key: string]: number}) {
  const val = 'test';
  if (input[val]) {
    logNumber(input[val]); // Argument of type 'number | undefined' is not assignable to parameter of type 'number'
  }
}

function doStuff3(input: Record<string, number>) {
  if (input[LOOKUP_KEY]) {
    logNumber(input[LOOKUP_KEY]); // Argument of type 'number | undefined' is not assignable to parameter of type 'number'
  }
}

function doStuff4(input: Record<string, number>) {
  if (input['test']) {
    logNumber(input['test']); // Works as expected
  }
}

🙁 Actual behavior

In doStuff, doStuffScopedConstant and doStuff3, a compiler error is thrown, even though I've checked that a value exists for that index. When directly using a string literal inline (see other examples), this works as expected.

🙂 Expected behavior

All of the provided examples should compile cleanly. Having a stored constant string literal used as an index accessor should work the same way as using the literal in-place.
I suspect that the compiler logic for noUncheckedIndexedAccess bails out w hen there's any variable used as an index accessor. I'd expect that if there's a const that's also typed as a string literal, it should be treated the same way as using a string literal.

@MartinJohns
Copy link
Contributor

#36230

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 2, 2021
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

3 participants