microsoft / microsoft/TypeScript

Error: Type cannot be used to index type after two indexes

Open
#21,760 14 comments 32 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Indexed Access Types
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 2.7.1, 2.8.0-dev.20180208

Search Terms:
Type cannot be used to index type

Code

interface IExample {
    foo: {
        bar: {
            baz: number;
        }
    }
}

type F = <
    name extends keyof IExample,
    val extends keyof IExample[name]
>() => IExample[name][val]['baz']; // ← Type '"baz"' cannot be used to index type 'IExample[name][val]'.

Expected behavior:
In version 2.7.0-dev.20171115 this code was checking without errors.

Actual behavior:
Now in throws error: Type '"baz"' cannot be used to index type 'IExample[name][val]'.

Playground Link:
https://www.typescriptlang.org/play/index.html#src=interface%20IExample%20%7B%0D%0A%20%20%20%20foo%3A%20%7B%0D%0A%20%20%20%20%20%20%20%20bar%3A%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20baz%3A%20number%3B%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Atype%20F%20%3D%20%3C%0D%0A%20%20%20%20name%20extends%20keyof%20IExample%2C%0D%0A%20%20%20%20val%20extends%20keyof%20IExample%5Bname%5D%0D%0A%3E()%20%3D%3E%20IExample%5Bname%5D%5Bval%5D%5B'baz'%5D%3B

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the TypeScript Playground reproduction in the issue and compare its behavior across the reported compiler versions, focusing on the generic indexed-access expression IExample[name][val]['baz']. Trace the type-checking path for the second indexed access and verify that the example checks without errors when the fix is complete.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.