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

Array.indexOf - searchElement is too Restrictive? #37434

Closed
halfnibble opened this issue Mar 17, 2020 · 3 comments
Closed

Array.indexOf - searchElement is too Restrictive? #37434

halfnibble opened this issue Mar 17, 2020 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@halfnibble
Copy link

TypeScript Version: 3.8.3

Search Terms:
indexOf, too restrictive, searchElement

Expected behavior:
I expect Array.indexOf() to allow a searchElement with a type that includes and extends beyond the array. E.g. If the haystack is [2, 3], then I expect to the needle could be of type 1 | 2 | 3 and that a value of -1 would be returned if the needle is 1. But I do not expect a compiler error.

Actual behavior:
Compiler error.
Argument of type 'CoolNumbers' is not assignable to parameter of type '2 | 3'.
Type '1' is not assignable to type '2 | 3'. (2345)

Related Issues:
#29290
#35257

Code

type CoolNumbers = 1 | 2 | 3;

const B: CoolNumbers = 2;
const C: CoolNumbers = 3;

const A: CoolNumbers = Math.floor(Math.random() * 3 + 1) as CoolNumbers;

if ([B, C].indexOf(A) === -1) {  // <--- Error here.
    console.log('My favorite 2 numbers.');
} else {
    console.log("it's 1");
}
Output
"use strict";
const B = 2;
const C = 3;
const A = Math.floor(Math.random() * 3 + 1);
if ([B, C].indexOf(A) === -1) {
    console.log('My favorite 2 numbers.');
}
else {
    console.log("it's 1");
}
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "moduleResolution": 2,
    "target": "ES2017",
    "jsx": "React",
    "module": "ESNext"
  }
}

Playground Link: Provided

@RyanCavanaugh
Copy link
Member

Root cause is #14520

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 17, 2020
@halfnibble
Copy link
Author

Thanks for the root cause! I honestly don't understand all the terminology in that issue, but I feel like this is a fairly common use case for indexOf. Please let me know if I'm doing something wrong.

My current workaround is to typecast A as any to get past compilation errors.

@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

3 participants