@@ -10523,7 +10523,8 @@ namespace ts {
10523
10523
}
10524
10524
10525
10525
function createUnionOrIntersectionProperty(containingType: UnionOrIntersectionType, name: __String): Symbol | undefined {
10526
- const propSet = createMap<Symbol>();
10526
+ let singleProp: Symbol | undefined;
10527
+ let propSet: Map<Symbol> | undefined;
10527
10528
let indexTypes: Type[] | undefined;
10528
10529
const isUnion = containingType.flags & TypeFlags.Union;
10529
10530
const excludeModifiers = isUnion ? ModifierFlags.NonPublicAccessibilityModifier : 0;
@@ -10543,9 +10544,18 @@ namespace ts {
10543
10544
else {
10544
10545
optionalFlag &= prop.flags;
10545
10546
}
10546
- const id = "" + getSymbolId(prop);
10547
- if (!propSet.has(id)) {
10548
- propSet.set(id, prop);
10547
+ if (!singleProp) {
10548
+ singleProp = prop;
10549
+ }
10550
+ else if (prop !== singleProp) {
10551
+ if (!propSet) {
10552
+ propSet = createMap<Symbol>();
10553
+ propSet.set("" + getSymbolId(singleProp), singleProp);
10554
+ }
10555
+ const id = "" + getSymbolId(prop);
10556
+ if (!propSet.has(id)) {
10557
+ propSet.set(id, prop);
10558
+ }
10549
10559
}
10550
10560
checkFlags |= (isReadonlySymbol(prop) ? CheckFlags.Readonly : 0) |
10551
10561
(!(modifiers & ModifierFlags.NonPublicAccessibilityModifier) ? CheckFlags.ContainsPublic : 0) |
@@ -10572,13 +10582,13 @@ namespace ts {
10572
10582
}
10573
10583
}
10574
10584
}
10575
- if (!propSet.size ) {
10585
+ if (!singleProp ) {
10576
10586
return undefined;
10577
10587
}
10578
- const props = arrayFrom(propSet.values());
10579
- if (props.length === 1 && !(checkFlags & CheckFlags.ReadPartial) && !indexTypes) {
10580
- return props[0];
10588
+ if (!propSet && !(checkFlags & CheckFlags.ReadPartial) && !indexTypes) {
10589
+ return singleProp;
10581
10590
}
10591
+ const props = propSet ? arrayFrom(propSet.values()) : [singleProp];
10582
10592
let declarations: Declaration[] | undefined;
10583
10593
let firstType: Type | undefined;
10584
10594
let nameType: Type | undefined;
@@ -14275,7 +14285,7 @@ namespace ts {
14275
14285
function instantiateType(type: Type, mapper: TypeMapper | undefined): Type;
14276
14286
function instantiateType(type: Type | undefined, mapper: TypeMapper | undefined): Type | undefined;
14277
14287
function instantiateType(type: Type | undefined, mapper: TypeMapper | undefined): Type | undefined {
14278
- if (!type || ! mapper) {
14288
+ if (!( type && mapper && couldContainTypeVariables(type)) ) {
14279
14289
return type;
14280
14290
}
14281
14291
if (instantiationDepth === 50 || instantiationCount >= 5000000) {
@@ -14311,37 +14321,23 @@ namespace ts {
14311
14321
}
14312
14322
if (flags & TypeFlags.Object) {
14313
14323
const objectFlags = (<ObjectType>type).objectFlags;
14314
- if (objectFlags & ObjectFlags.Anonymous) {
14315
- // If the anonymous type originates in a declaration of a function, method, class, or
14316
- // interface, in an object type literal, or in an object literal expression, we may need
14317
- // to instantiate the type because it might reference a type parameter.
14318
- return couldContainTypeVariables(type) ?
14319
- getObjectTypeInstantiation(<AnonymousType>type, mapper) : type;
14320
- }
14321
- if (objectFlags & ObjectFlags.Mapped) {
14322
- return getObjectTypeInstantiation(<AnonymousType>type, mapper);
14323
- }
14324
- if (objectFlags & ObjectFlags.Reference) {
14325
- if ((<TypeReference>type).node) {
14326
- return getObjectTypeInstantiation(<TypeReference>type, mapper);
14324
+ if (objectFlags & (ObjectFlags.Reference | ObjectFlags.Anonymous | ObjectFlags.Mapped)) {
14325
+ if (objectFlags & ObjectFlags.Reference && !((<TypeReference>type).node)) {
14326
+ const resolvedTypeArguments = (<TypeReference>type).resolvedTypeArguments;
14327
+ const newTypeArguments = instantiateTypes(resolvedTypeArguments, mapper);
14328
+ return newTypeArguments !== resolvedTypeArguments ? createTypeReference((<TypeReference>type).target, newTypeArguments) : type;
14327
14329
}
14328
- const resolvedTypeArguments = (<TypeReference>type).resolvedTypeArguments;
14329
- const newTypeArguments = instantiateTypes(resolvedTypeArguments, mapper);
14330
- return newTypeArguments !== resolvedTypeArguments ? createTypeReference((<TypeReference>type).target, newTypeArguments) : type;
14330
+ return getObjectTypeInstantiation(<TypeReference | AnonymousType | MappedType>type, mapper);
14331
14331
}
14332
14332
return type;
14333
14333
}
14334
- if ((flags & TypeFlags.Intersection) || (flags & TypeFlags.Union && !(flags & TypeFlags.Primitive))) {
14335
- if (!couldContainTypeVariables(type)) {
14336
- return type;
14337
- }
14334
+ if (flags & TypeFlags.UnionOrIntersection) {
14338
14335
const types = (<UnionOrIntersectionType>type).types;
14339
14336
const newTypes = instantiateTypes(types, mapper);
14340
- return newTypes === types
14341
- ? type
14342
- : (flags & TypeFlags.Intersection)
14343
- ? getIntersectionType(newTypes, type.aliasSymbol, instantiateTypes(type.aliasTypeArguments, mapper))
14344
- : getUnionType(newTypes, UnionReduction.Literal, type.aliasSymbol, instantiateTypes(type.aliasTypeArguments, mapper));
14337
+ return newTypes === types ? type :
14338
+ flags & TypeFlags.Intersection ?
14339
+ getIntersectionType(newTypes, type.aliasSymbol, instantiateTypes(type.aliasTypeArguments, mapper)) :
14340
+ getUnionType(newTypes, UnionReduction.Literal, type.aliasSymbol, instantiateTypes(type.aliasTypeArguments, mapper));
14345
14341
}
14346
14342
if (flags & TypeFlags.Index) {
14347
14343
return getIndexType(instantiateType((<IndexType>type).type, mapper));
@@ -18388,16 +18384,25 @@ namespace ts {
18388
18384
return !!(objectFlags & ObjectFlags.CouldContainTypeVariables);
18389
18385
}
18390
18386
const result = !!(type.flags & TypeFlags.Instantiable ||
18391
- objectFlags & ObjectFlags.Reference && ((<TypeReference>type).node || forEach(getTypeArguments(<TypeReference>type), couldContainTypeVariables)) ||
18392
- objectFlags & ObjectFlags.Anonymous && type.symbol && type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.TypeLiteral | SymbolFlags.ObjectLiteral) && type.symbol.declarations ||
18393
- objectFlags & (ObjectFlags.Mapped | ObjectFlags.ObjectRestType) ||
18394
- type.flags & TypeFlags.UnionOrIntersection && !(type.flags & TypeFlags.EnumLiteral) && some((<UnionOrIntersectionType>type).types, couldContainTypeVariables));
18387
+ type.flags & TypeFlags.Object && !isNonGenericTopLevelType(type) && (
18388
+ objectFlags & ObjectFlags.Reference && ((<TypeReference>type).node || forEach(getTypeArguments(<TypeReference>type), couldContainTypeVariables)) ||
18389
+ objectFlags & ObjectFlags.Anonymous && type.symbol && type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.TypeLiteral | SymbolFlags.ObjectLiteral) && type.symbol.declarations ||
18390
+ objectFlags & (ObjectFlags.Mapped | ObjectFlags.ObjectRestType)) ||
18391
+ type.flags & TypeFlags.UnionOrIntersection && !(type.flags & TypeFlags.EnumLiteral) && !isNonGenericTopLevelType(type) && some((<UnionOrIntersectionType>type).types, couldContainTypeVariables));
18395
18392
if (type.flags & TypeFlags.ObjectFlagsType) {
18396
18393
(<ObjectFlagsType>type).objectFlags |= ObjectFlags.CouldContainTypeVariablesComputed | (result ? ObjectFlags.CouldContainTypeVariables : 0);
18397
18394
}
18398
18395
return result;
18399
18396
}
18400
18397
18398
+ function isNonGenericTopLevelType(type: Type) {
18399
+ if (type.aliasSymbol && !type.aliasTypeArguments) {
18400
+ const declaration = getDeclarationOfKind(type.aliasSymbol, SyntaxKind.TypeAliasDeclaration);
18401
+ return !!(declaration && findAncestor(declaration.parent, n => n.kind === SyntaxKind.SourceFile ? true : n.kind === SyntaxKind.ModuleDeclaration ? false : "quit"));
18402
+ }
18403
+ return false;
18404
+ }
18405
+
18401
18406
function isTypeParameterAtTopLevel(type: Type, typeParameter: TypeParameter): boolean {
18402
18407
return !!(type === typeParameter ||
18403
18408
type.flags & TypeFlags.UnionOrIntersection && some((<UnionOrIntersectionType>type).types, t => isTypeParameterAtTopLevel(t, typeParameter)) ||
0 commit comments