From 7d7f4680c69c2a345a7adedb7a8fe3efb52aeb44 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sun, 7 Apr 2024 17:36:21 -0700 Subject: [PATCH] Add IndexFlags.FromIntersection --- src/compiler/checker.ts | 12 ++++++------ src/compiler/types.ts | 9 +++------ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 8b1477961ab..c0572721426 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -18075,9 +18075,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { } function getIndexTypeForGenericType(type: InstantiableType | UnionOrIntersectionType, indexFlags: IndexFlags) { - return indexFlags & IndexFlags.StringsOnly ? - type.resolvedStringIndexType || (type.resolvedStringIndexType = createIndexType(type, IndexFlags.StringsOnly)) : - type.resolvedIndexType || (type.resolvedIndexType = createIndexType(type, IndexFlags.None)); + const persistentFlags = indexFlags & (IndexFlags.StringsOnly | IndexFlags.FromIntersection); + const key = `K${getTypeId(type)},${persistentFlags}`; + return getCachedType(key) ?? setCachedType(key, createIndexType(type, persistentFlags)); } /** @@ -18211,11 +18211,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { return isNoInferType(type) ? getNoInferType(getIndexType((type as SubstitutionType).baseType, indexFlags)) : shouldDeferIndexType(type, indexFlags) ? getIndexTypeForGenericType(type as InstantiableType | UnionOrIntersectionType, indexFlags) : type.flags & TypeFlags.Union ? getIntersectionType(map((type as UnionType).types, t => getIndexType(t, indexFlags))) : - type.flags & TypeFlags.Intersection ? getUnionType(map((type as IntersectionType).types, t => getIndexType(t, indexFlags))) : + type.flags & TypeFlags.Intersection ? getUnionType(map((type as IntersectionType).types, t => getIndexType(t, indexFlags | IndexFlags.FromIntersection))) : getObjectFlags(type) & ObjectFlags.Mapped ? getIndexTypeForMappedType(type as MappedType, indexFlags) : type === wildcardType ? wildcardType : type.flags & TypeFlags.Unknown ? neverType : - type.flags & (TypeFlags.Any | TypeFlags.Never) ? stringNumberSymbolType : + type.flags & (TypeFlags.Any | TypeFlags.Never) || type.flags & TypeFlags.Nullable && indexFlags & IndexFlags.FromIntersection ? stringNumberSymbolType : getLiteralTypeFromProperties(type, (indexFlags & IndexFlags.NoIndexSignatures ? TypeFlags.StringLiteral : TypeFlags.StringLike) | (indexFlags & IndexFlags.StringsOnly ? 0 : TypeFlags.NumberLike | TypeFlags.ESSymbolLike), indexFlags === IndexFlags.None); } @@ -20286,7 +20286,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { getUnionType(newTypes, UnionReduction.Literal, newAliasSymbol, newAliasTypeArguments); } if (flags & TypeFlags.Index) { - return getIndexType(instantiateType((type as IndexType).type, mapper)); + return getIndexType(instantiateType((type as IndexType).type, mapper), (type as IndexType).indexFlags); } if (flags & TypeFlags.TemplateLiteral) { return getTemplateLiteralType((type as TemplateLiteralType).texts, instantiateTypes((type as TemplateLiteralType).types, mapper)); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 412686d00f4..8f76103c5d1 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -6569,10 +6569,6 @@ export interface SyntheticDefaultModuleType extends Type { export interface InstantiableType extends Type { /** @internal */ resolvedBaseConstraint?: Type; - /** @internal */ - resolvedIndexType?: IndexType; - /** @internal */ - resolvedStringIndexType?: IndexType; } // Type parameters (TypeFlags.TypeParameter) @@ -6629,8 +6625,9 @@ export type TypeVariable = TypeParameter | IndexedAccessType; export const enum IndexFlags { None = 0, StringsOnly = 1 << 0, - NoIndexSignatures = 1 << 1, - NoReducibleCheck = 1 << 2, + FromIntersection = 1 << 1, + NoIndexSignatures = 1 << 2, + NoReducibleCheck = 1 << 3, } // keyof T types (TypeFlags.Index)