From 2376e30410e112689acbd3268ee94aa113546c56 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 18 Nov 2016 13:30:56 -0800 Subject: [PATCH] Support apparent types for T[K] indexed access types --- src/compiler/checker.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f6689471cd8..024c3d4a983 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4666,13 +4666,22 @@ namespace ts { return type.resolvedApparentType; } + /** + * The apparent type of an indexed access T[K] is the type of T's string index signature, if any. + */ + function getApparentTypeOfIndexedAccess(type: IndexedAccessType) { + return getIndexTypeOfType(getApparentType(type.objectType), IndexKind.String) || type; + } + /** * For a type parameter, return the base constraint of the type parameter. For the string, number, * boolean, and symbol primitive types, return the corresponding object types. Otherwise return the * type itself. Note that the apparent type of a union type is the union type itself. */ function getApparentType(type: Type): Type { - const t = type.flags & TypeFlags.TypeParameter ? getApparentTypeOfTypeParameter(type) : type; + const t = type.flags & TypeFlags.TypeParameter ? getApparentTypeOfTypeParameter(type) : + type.flags & TypeFlags.IndexedAccess ? getApparentTypeOfIndexedAccess(type) : + type; return t.flags & TypeFlags.StringLike ? globalStringType : t.flags & TypeFlags.NumberLike ? globalNumberType : t.flags & TypeFlags.BooleanLike ? globalBooleanType :