question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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 IExample { foo%3A { bar%3A { baz%3A number%3B } } } type F %3D < name extends keyof IExample%2C val extends keyof IExample[name] >() %3D> IExample[name][val][‘baz’]%3B

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:19
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

17reactions
wycatscommented, Apr 15, 2018

Actually, it’s really weird! I’m getting type errors, but TS is getting the derived type right anyway!

10reactions
moretolikecommented, Nov 29, 2018

ERROR in node_modules/@amcharts/amcharts4/.internal/charts/types/XYChart.d.ts(258,33): error TS2344: Type ‘this[“_xAxisRendererType”]’ does not satisfy the constraint ‘AxisRenderer’. Type ‘AxisRendererX’ is not assignable to type ‘AxisRenderer’. Types of property ‘events’ are incompatible. Type ‘SpriteEventDispatcher<AMEvent<AxisRendererX, IAxisRendererXEvents>>’ is not assignable to type ‘SpriteEventDispatcher<AMEvent<this[“_xAxisRendererType”], this[“_xAxisRendererType”][“_events”]>>’. Type ‘AMEvent<AxisRendererX, IAxisRendererXEvents>’ is not assignable to type ‘AMEvent<this[“_xAxisRendererType”], this[“_xAxisRendererType”][“_events”]>’. node_modules/@amcharts/amcharts4/.internal/charts/types/XYChart.d.ts(264,33): error TS2344: Type ‘this[“_yAxisRendererType”]’ does not satisfy the constraint ‘AxisRenderer’. Type ‘AxisRendererY’ is not assignable to type ‘AxisRenderer’. Types of property ‘events’ are incompatible. Type ‘SpriteEventDispatcher<AMEvent<AxisRendererY, IAxisRendererYEvents>>’ is not assignable to type ‘SpriteEventDispatcher<AMEvent<this[“_yAxisRendererType”], this[“_yAxisRendererType”][“_events”]>>’. Type ‘AMEvent<AxisRendererY, IAxisRendererYEvents>’ is not assignable to type ‘AMEvent<this[“_yAxisRendererType”], this[“_yAxisRendererType”][“_events”]>’. node_modules/@amcharts/amcharts4/.internal/charts/types/XYChart.d.ts(503,31): error TS2344: Type ‘this[“_xAxisRendererType”]’ does not satisfy the constraint ‘AxisRenderer’. node_modules/@amcharts/amcharts4/.internal/charts/types/XYChart.d.ts(513,31): error TS2344: Type ‘this[“_yAxisRendererType”]’ does not satisfy the constraint ‘AxisRenderer’. node_modules/@amcharts/amcharts4/.internal/core/utils/Object.d.ts(60,117): error TS2536: Type ‘Key’ cannot be used to index type ‘Object’. node_modules/@amcharts/amcharts4/.internal/core/utils/Object.d.ts(67,109): error TS2536: Type ‘Key’ cannot be used to index type ‘Object’. node_modules/@amcharts/amcharts4/.internal/core/utils/Object.d.ts(76,116): error TS2536: Type ‘Key’ cannot be used to index type ‘Object’. node_modules/@amcharts/amcharts4/.internal/core/utils/Type.d.ts(25,32): error TS2304: Cannot find name ‘Extract’.

Please… Help Me…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript Error: type 'string' can't be used to index type X
I would suggest looking at the answer from LCC below. The problem with Record<> and unconstrained Index Signatures is that you are widening...
Read more >
string can't be used to index type - You.com | The AI Search ...
The "Type 'String' cannot be used as an index type" TypeScript error occurs when we use the String type instead of string (lowercase...
Read more >
Type 'X' cannot be used as an index type in TypeScript
The error "Type cannot be used as an index type" occurs when we try to use a type that cannot be used to...
Read more >
Documentation - Indexed Access Types - TypeScript
const key = "age";. type Age = Person [ key ];. Type 'key' cannot be used as an index type. ... 2749Type 'key'...
Read more >
Solved - "Element implicitly has an 'any' type because ...
The error "Element implicitly has an 'any' type because expression of type 'number' can't be used to index type" occurs when we try...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found