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

Wrong indexOf type in array #31022

Closed
aczekajski opened this issue Apr 18, 2019 · 2 comments
Closed

Wrong indexOf type in array #31022

aczekajski opened this issue Apr 18, 2019 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@aczekajski
Copy link

aczekajski commented Apr 18, 2019

TypeScript Version: 3.4.0 and 3.5.0-dev.20190418

Search Terms: indexOf, array union, ts(2345)

Code
There seems to be an error in type of indexOf function in Array interface. It seems to be too strict, not allowing to search for values that might or might not be inside the array.

type AB = 'A' | 'B';
type CD = 'C' | 'D';
type ABCD = AB | CD;

const fn1 = (t: AB[], v: ABCD) => t.indexOf(v); // v might be "B" so it is a perfectly valid indexOf usage

const fn2 = (t: AB[], v: CD) => t.indexOf(v); // AB and CD have nothing in common, this might be an error

const fn3 = (t: ABCD[], v: AB) => t.indexOf(v); // this is ok

Expected behavior:
indexOf inside fn1 shouldn't throw an error because there is a non-empty intersection between array values type and searched value type

Actual behavior:
indexOf inside fn1 throws the following error:

Argument of type 'ABCD' is not assignable to parameter of type 'AB'.
  Type '"C"' is not assignable to type 'AB'.ts(2345)

Playground Link: TypeScript Playground

@RyanCavanaugh
Copy link
Member

Effective duplicate of #26255

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 18, 2019
@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