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

The value check by index is ignored in the right-hand side of the logical conjunction #56647

Closed
s1owjke opened this issue Dec 2, 2023 · 1 comment

Comments

@s1owjke
Copy link

s1owjke commented Dec 2, 2023

πŸ”Ž Search Terms

  • is not assignable to parameter of type
  • noUncheckedIndexedAccess &&
  • logical conjunction

πŸ•— Version & Regression Information

  • This occurs since 4.1 when the noUncheckedIndexedAccess option was added

⏯ Playground Link

https://www.typescriptlang.org/play?noUncheckedIndexedAccess=true&target=7&module=6&noFallthroughCasesInSwitch=true&ts=5.4.0-dev.20231202#code/MYewdgzgLgBA0gUQJoH0BKCDiCAaMC8MA9AHoC0ZAFAHQBUAlEQNwBQoksAhgE4DmEALhjRuASzC8A2gF0CMSQCIKnYFFEA3AKYKANDCVkwAVwC2AI03dd+gIwLprNuGgxxAE00APOQAZWogDNKHn5Jdy8YAGoYG1kAMjj4ZHQsXGooTWhgvggwsA9vaNj6ehgAbwBfFicOGDAvKABJfIjCcMKY-wCYbND6zyaWz3jExFQMbBx0zKhe3P7BgukS8qqalwXGjJM5EPmG5qWuns3tmASk8dSpjKzTzRMVyqA

πŸ’» Code

const KEY_REGEX = /^--(.*)/;
const args: string[] = ["--active", "--number", "1"];

// error
const index = 0;
if(args[index + 1] && KEY_REGEX.test(args[index + 1])) {}

// error
const nextIndex = index + 1;
if (args[nextIndex] && KEY_REGEX.test(args[nextIndex])) {}

// no error - can be used as workaround
const nextItem = args[nextIndex];
if (nextItem && KEY_REGEX.test(nextItem)) {}

πŸ™ Actual behavior

If we do an check by index, we get the following error:

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'. (2345)

πŸ™‚ Expected behavior

No error

Additional information about the issue

This only happens if the noUncheckedIndexedAccess option is set to true

@MartinJohns
Copy link
Contributor

Duplicate of #56389.

@s1owjke s1owjke closed this as completed Dec 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants