Skip to content

Type inferrence breaks on nested generic indexes #21770

Description

TypeScript Version:

2.7.1
2.8.0-dev.20180208

Search Terms:

inferred generic index

Code

typeDict<Textendsstring>={[keyinT]: number}typeDictDict<Vextendsstring,Textendsstring>={[keyinV]: Dict<T>}functionf<Vextendsstring,Textendsstring>(dd: DictDict<V,T>,k1: V,k2: T): number{// Type 'DictDict<V, T>[V][T]' is not assignable to type 'number'.returndd[k1][k2]}// worksfunctionf1<Vextendsstring,Textendsstring>(dd: DictDict<V,T>,k1: V,k2: T): number{constd: Dict<T>=dd[k1]returnd[k2]}

Expected behavior:

The code above works on typescript 2.6.2, where dd[k1][k2] in f appears to correctly infer number as the type.

Actual behavior:

In >= 2.7.1, typescript now appears to need help figuring out the type of dd[k1].

Playground Link:

https://www.typescriptlang.org/play/#src=type%20Dict%3CT%20extends%20string%3E%20%3D%20%7B%20%5Bkey%20in%20T%5D%3A%20number%20%7D%0D%0Atype%20DictDict%3CV%20extends%20string%2C%20T%20extends%20string%3E%20%3D%20%7B%20%5Bkey%20in%20V%5D%3A%20Dict%3CT%3E%20%7D%0D%0A%0D%0Afunction%20f%3CV%20extends%20string%2C%20T%20extends%20string%3E(dd%3A%20DictDict%3CV%2C%20T%3E%2C%20k1%3A%20V%2C%20k2%3A%20T)%3A%20number%20%7B%0D%0A%20%20%20%20%2F%2F%20Type%20'DictDict%3CV%2C%20T%3E%5BV%5D%5BT%5D'%20is%20not%20assignable%20to%20type%20'number'.%0D%0A%20%20%20%20return%20dd%5Bk1%5D%5Bk2%5D%0D%0A%7D%0D%0A%0D%0A%2F%2F%20works%0D%0Afunction%20f1%3CV%20extends%20string%2C%20T%20extends%20string%3E(dd%3A%20DictDict%3CV%2C%20T%3E%2C%20k1%3A%20V%2C%20k2%3A%20T)%3A%20number%20%7B%0D%0A%20%20%20%20const%20d%3A%20Dict%3CT%3E%20%3D%20dd%5Bk1%5D%0D%0A%20%20%20%20return%20d%5Bk2%5D%0D%0A%7D

Related Issues:

21368

Metadata

Metadata

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions