Skip to content

Commit 9e4e215

Browse files
committed
Revise IndexType to have stringsOnly property
1 parent 254782c commit 9e4e215

2 files changed

Lines changed: 21 additions & 20 deletions

File tree

src/compiler/checker.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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));

src/compiler/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3761,7 +3761,7 @@ namespace ts {
37613761
/* @internal */
37623762
resolvedIndexType: IndexType;
37633763
/* @internal */
3764-
resolvedDeclaredIndexType: IndexType;
3764+
resolvedStringIndexType: IndexType;
37653765
/* @internal */
37663766
resolvedBaseConstraint: Type;
37673767
/* @internal */
@@ -3850,7 +3850,7 @@ namespace ts {
38503850
/* @internal */
38513851
resolvedIndexType?: IndexType;
38523852
/* @internal */
3853-
resolvedDeclaredIndexType?: IndexType;
3853+
resolvedStringIndexType?: IndexType;
38543854
}
38553855

38563856
// Type parameters (TypeFlags.TypeParameter)
@@ -3882,9 +3882,9 @@ namespace ts {
38823882

38833883
// keyof T types (TypeFlags.Index)
38843884
export interface IndexType extends InstantiableType {
3885-
/* @internal */
3886-
isDeclaredType?: boolean;
38873885
type: InstantiableType | UnionOrIntersectionType;
3886+
/* @internal */
3887+
stringsOnly: boolean;
38883888
}
38893889

38903890
export interface ConditionalRoot {

0 commit comments

Comments
 (0)