Typescript: Type 'string' cannot be used to index type 'T' - after upgrade from 3.4 to 3.5

Created on 4 Aug 2019  ·  5Comments  ·  Source: microsoft/TypeScript

TypeScript Version: 3.6.0-dev.20190804 as well as 3.5.3. It was fine in version 3.4.5

Code

Here's a minimal reproduction tested using tsc:

class ParentType {
  [key: string]: string|number;
}

class ChildType extends ParentType {
  someKey: number;
}

class MyClass<T extends ParentType> {
  field: T;

  doStuff() {
    this.field['someKey'] = 123;
  }
}

error TS2536: Type 'string' cannot be used to index type 'T'.

Expected behavior:

There shouldn't be breaking changes from one minor version to another

Actual behavior:

Upgrading my app from 3.4.5 to 3.5.3 caused a build error

Real code: https://github.com/azerothcore/Keira3/pull/12

Duplicate

Most helpful comment

if this is an intended change, how am I supposed to change my code to comply with the new version?

also @RyanCavanaugh what is the duplicating issue?

All 5 comments

Changed by #30769. Specifically see:

Given a type variable T with a constraint C, when an indexed access T[K] occurs on the target side of a type relationship, index signatures in C are now ignored. This is because a type argument for T isn't actually required to have an index signature, it is just required to have properties with matching types.

then shouldn't the major version be updated? e.g. 4.0

14116

if this is an intended change, how am I supposed to change my code to comply with the new version?

also @RyanCavanaugh what is the duplicating issue?

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Was this page helpful?
0 / 5 - 0 ratings