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 <generic> cannot be used to index type <array> #15768

Closed
KiaraGrouwstra opened this issue May 11, 2017 · 5 comments
Closed

Type <generic> cannot be used to index type <array> #15768

KiaraGrouwstra opened this issue May 11, 2017 · 5 comments
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@KiaraGrouwstra
Copy link
Contributor

TypeScript Version:
0 2.3.1-insiders.20170416 (Playground)

Code

type X<I extends number> = ['a'][I];

Expected behavior:
No error

Actual behavior:
Type 'I' cannot be used to index type '["a"]'.

I thought this may have worked before. Potential regression?

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label May 11, 2017
@WanderWang
Copy link

WanderWang commented Jun 19, 2017

I have a simular issue .

type ValueType = { 0: { name: string }, 36: { id: string } };
type KeyType = 0 | 36

function getValue<T extends KeyType>(key: T): ValueType[T] {
    let data: ValueType;
    let result = data[key.toString()];
    return result;
}
let a = getValue(0).name;
let b = getValue(36).id;

Expected behavior:
No error

Actual behavior:
Type 'T' cannot be used to index type 'ValueType'.

@KiaraGrouwstra
Copy link
Contributor Author

Temp workaround from here: throw the generic through another type that outputs a number.

@KiaraGrouwstra
Copy link
Contributor Author

KiaraGrouwstra commented Aug 11, 2017

Hm, not having as much luck with that workaround anymore on nightlies, maybe related to #17455.
That now makes this error account for the majority of errors in my type lib.

Here's another bigger example showing it fails when generics are involved even if separated by operations that would work:

type NumberToString = { [i: number]: string; };
type StringToNumber = {
  // [k: string]: number;
  '0': 0;
};

type A = NumberToString[StringToNumber[string]];
// -> ok, string

type B<T extends '0'> = NumberToString[StringToNumber[T]];
// -> Type 'StringToNumber[T]' cannot be used to index type 'NumberToString'.
type C<T extends '1'> = NumberToString[StringToNumber[T]];
// ^ if StringToNumber has no index, infers any
// ^ if StringToNumber has an index, "Type 'StringToNumber[T]' cannot be used to index type 'NumberToString'."

@KiaraGrouwstra
Copy link
Contributor Author

I think the cause here is checkIndexedAccessIndexType in the TS compiler's checker.ts, which does a TypeVariable check for the object type yet not for the index type.

KiaraGrouwstra added a commit to KiaraGrouwstra/TypeScript that referenced this issue Aug 13, 2017
@KiaraGrouwstra
Copy link
Contributor Author

PR at #17767.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

5 participants