@@ -8191,16 +8191,17 @@ namespace ts {
81918191 return links.resolvedType;
81928192 }
81938193
8194- function getIndexTypeForGenericType(type: InstantiableType | UnionOrIntersectionType, includeDeclaredTypes?: boolean) {
8195- const cacheLocation = includeDeclaredTypes ? "resolvedDeclaredIndexType" : "resolvedIndexType";
8196- if (!type[cacheLocation]) {
8197- type[cacheLocation] = <IndexType>createType(TypeFlags.Index);
8198- type[cacheLocation].type = type;
8199- if (includeDeclaredTypes) {
8200- type[cacheLocation].isDeclaredType = true;
8201- }
8202- }
8203- return type[cacheLocation];
8194+ function createIndexType(type: InstantiableType | UnionOrIntersectionType, stringsOnly: boolean) {
8195+ const result = <IndexType>createType(TypeFlags.Index);
8196+ result.type = type;
8197+ result.stringsOnly = stringsOnly;
8198+ return result;
8199+ }
8200+
8201+ function getIndexTypeForGenericType(type: InstantiableType | UnionOrIntersectionType, stringsOnly: boolean) {
8202+ return stringsOnly ?
8203+ type.resolvedStringIndexType || (type.resolvedStringIndexType = createIndexType(type, /*stringsOnly*/ true)) :
8204+ type.resolvedIndexType || (type.resolvedIndexType = createIndexType(type, /*stringsOnly*/ false));
82048205 }
82058206
82068207 function getLiteralTypeFromPropertyName(prop: Symbol, include: TypeFlags) {
@@ -8225,13 +8226,13 @@ namespace ts {
82258226 return numberIndexInfo !== enumNumberIndexInfo ? numberIndexInfo : undefined;
82268227 }
82278228
8228- function getIndexType(type: Type, includeDeclaredTypes?: boolean ): Type {
8229- return type.flags & TypeFlags.Intersection ? getUnionType(map((<IntersectionType>type).types, t => getIndexType(t, includeDeclaredTypes ))) :
8230- maybeTypeOfKind(type, TypeFlags.InstantiableNonPrimitive) ? getIndexTypeForGenericType(<InstantiableType | UnionOrIntersectionType>type, includeDeclaredTypes ) :
8229+ function getIndexType(type: Type, stringsOnly = keyofStringsOnly ): Type {
8230+ return type.flags & TypeFlags.Intersection ? getUnionType(map((<IntersectionType>type).types, t => getIndexType(t, stringsOnly ))) :
8231+ maybeTypeOfKind(type, TypeFlags.InstantiableNonPrimitive) ? getIndexTypeForGenericType(<InstantiableType | UnionOrIntersectionType>type, stringsOnly ) :
82318232 getObjectFlags(type) & ObjectFlags.Mapped ? getConstraintTypeFromMappedType(<MappedType>type) :
82328233 type === wildcardType ? wildcardType :
82338234 type.flags & TypeFlags.Any ? keyofConstraintType :
8234- keyofStringsOnly ? getIndexInfoOfType(type, IndexKind.String) ? stringType : getLiteralTypeFromPropertyNames(type, TypeFlags.StringLiteral) :
8235+ stringsOnly ? getIndexInfoOfType(type, IndexKind.String) ? stringType : getLiteralTypeFromPropertyNames(type, TypeFlags.StringLiteral) :
82358236 getIndexInfoOfType(type, IndexKind.String) ? getUnionType([stringType, numberType, getLiteralTypeFromPropertyNames(type, TypeFlags.UniqueESSymbol)]) :
82368237 getNonEnumNumberIndexInfo(type) ? getUnionType([numberType, getLiteralTypeFromPropertyNames(type, TypeFlags.StringLiteral | TypeFlags.UniqueESSymbol)]) :
82378238 getLiteralTypeFromPropertyNames(type, TypeFlags.StringOrNumberLiteralOrUnique);
@@ -10438,7 +10439,7 @@ namespace ts {
1043810439 // constraint of T.
1043910440 const constraint = getConstraintForRelation((<IndexType>target).type);
1044010441 if (constraint) {
10441- if (result = isRelatedTo(source, getIndexType(constraint, (target as IndexType).isDeclaredType ), reportErrors)) {
10442+ if (result = isRelatedTo(source, getIndexType(constraint, (target as IndexType).stringsOnly ), reportErrors)) {
1044210443 return result;
1044310444 }
1044410445 }
@@ -21010,7 +21011,7 @@ namespace ts {
2101021011 // Check if the index type is assignable to 'keyof T' for the object type.
2101121012 const objectType = (<IndexedAccessType>type).objectType;
2101221013 const indexType = (<IndexedAccessType>type).indexType;
21013- if (isTypeAssignableTo(indexType, getIndexType(objectType, /*includeDeclaredTypes */ true ))) {
21014+ if (isTypeAssignableTo(indexType, getIndexType(objectType, /*stringsOnly */ false ))) {
2101421015 if (accessNode.kind === SyntaxKind.ElementAccessExpression && isAssignmentTarget(accessNode) &&
2101521016 getObjectFlags(objectType) & ObjectFlags.Mapped && getMappedTypeModifiers(<MappedType>objectType) & MappedTypeModifiers.IncludeReadonly) {
2101621017 error(accessNode, Diagnostics.Index_signature_in_type_0_only_permits_reading, typeToString(objectType));
0 commit comments