Skip to content

Commit 8c70109

Browse files
committed
Turn 3 type flags into properties
1. Instantiated (only modifies anonymous types) 2. ObjectLiteralWithComputedProperties (only modifies [resolved] object types) 3. ThisType (only modifies type parameters) This is needed for object spread and rest, which will each need a type flag. There are 4-5 other likely targets for removal, and I may remove those later.
1 parent 60ab007 commit 8c70109

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

src/compiler/checker.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,7 +2164,7 @@ namespace ts {
21642164
? "any"
21652165
: (<IntrinsicType>type).intrinsicName);
21662166
}
2167-
else if (type.flags & TypeFlags.ThisType) {
2167+
else if (type.flags & TypeFlags.TypeParameter && (type as TypeParameter).isThisType) {
21682168
if (inObjectTypeLiteral) {
21692169
writer.reportInaccessibleThisError();
21702170
}
@@ -3155,9 +3155,9 @@ namespace ts {
31553155
}
31563156

31573157
// Return the type implied by an object binding pattern
3158-
function getTypeFromObjectBindingPattern(pattern: ObjectBindingPattern, includePatternInType: boolean, reportErrors: boolean): Type {
3158+
function getTypeFromObjectBindingPattern(pattern: ObjectBindingPattern, includePatternInType: boolean, reportErrors: boolean): ResolvedType {
31593159
const members = createMap<Symbol>();
3160-
let hasComputedProperties = false;
3160+
let hasComputedProperties: boolean;
31613161
forEach(pattern.elements, e => {
31623162
const name = e.propertyName || <Identifier>e.name;
31633163
if (isComputedNonLiteralName(name)) {
@@ -3177,9 +3177,7 @@ namespace ts {
31773177
if (includePatternInType) {
31783178
result.pattern = pattern;
31793179
}
3180-
if (hasComputedProperties) {
3181-
result.flags |= TypeFlags.ObjectLiteralPatternWithComputedProperties;
3182-
}
3180+
result.inObjectLiteralPatternWithComputedProperties = hasComputedProperties;
31833181
return result;
31843182
}
31853183

@@ -3765,7 +3763,8 @@ namespace ts {
37653763
(<GenericType>type).instantiations[getTypeListId(type.typeParameters)] = <GenericType>type;
37663764
(<GenericType>type).target = <GenericType>type;
37673765
(<GenericType>type).typeArguments = type.typeParameters;
3768-
type.thisType = <TypeParameter>createType(TypeFlags.TypeParameter | TypeFlags.ThisType);
3766+
type.thisType = <TypeParameter>createType(TypeFlags.TypeParameter);
3767+
type.thisType.isThisType = true;
37693768
type.thisType.symbol = symbol;
37703769
type.thisType.constraint = type;
37713770
}
@@ -4414,7 +4413,7 @@ namespace ts {
44144413
* boolean, and symbol primitive types, return the corresponding object types. Otherwise return the
44154414
* type itself. Note that the apparent type of a union type is the union type itself.
44164415
*/
4417-
function getApparentType(type: Type): Type {
4416+
function getApparentType(type: Type): ObjectType {
44184417
if (type.flags & TypeFlags.TypeParameter) {
44194418
type = getApparentTypeOfTypeParameter(<TypeParameter>type);
44204419
}
@@ -4967,7 +4966,7 @@ namespace ts {
49674966

49684967
function hasConstraintReferenceTo(type: Type, target: TypeParameter): boolean {
49694968
let checked: Type[];
4970-
while (type && !(type.flags & TypeFlags.ThisType) && type.flags & TypeFlags.TypeParameter && !contains(checked, type)) {
4969+
while (type && type.flags & TypeFlags.TypeParameter && !((type as TypeParameter).isThisType) && !contains(checked, type)) {
49714970
if (type === target) {
49724971
return true;
49734972
}
@@ -5330,7 +5329,8 @@ namespace ts {
53305329
type.instantiations[getTypeListId(type.typeParameters)] = <GenericType>type;
53315330
type.target = <GenericType>type;
53325331
type.typeArguments = type.typeParameters;
5333-
type.thisType = <TypeParameter>createType(TypeFlags.TypeParameter | TypeFlags.ThisType);
5332+
type.thisType = <TypeParameter>createType(TypeFlags.TypeParameter);
5333+
type.thisType.isThisType = true;
53345334
type.thisType.constraint = type;
53355335
type.declaredProperties = properties;
53365336
type.declaredCallSignatures = emptyArray;
@@ -5920,7 +5920,8 @@ namespace ts {
59205920
mapper.instantiations = [];
59215921
}
59225922
// Mark the anonymous type as instantiated such that our infinite instantiation detection logic can recognize it
5923-
const result = <AnonymousType>createObjectType(TypeFlags.Anonymous | TypeFlags.Instantiated, type.symbol);
5923+
const result = <AnonymousType>createObjectType(TypeFlags.Anonymous, type.symbol);
5924+
result.isInstantiated = true;
59245925
result.target = type;
59255926
result.mapper = mapper;
59265927
result.aliasSymbol = type.aliasSymbol;
@@ -5992,7 +5993,7 @@ namespace ts {
59925993
// instantiation.
59935994
return type.symbol &&
59945995
type.symbol.flags & (SymbolFlags.Function | SymbolFlags.Method | SymbolFlags.Class | SymbolFlags.TypeLiteral | SymbolFlags.ObjectLiteral) &&
5995-
(type.flags & TypeFlags.Instantiated || isSymbolInScopeOfMappedTypeParameter(type.symbol, mapper)) ?
5996+
((type as AnonymousType).isInstantiated || isSymbolInScopeOfMappedTypeParameter(type.symbol, mapper)) ?
59965997
instantiateAnonymousType(<AnonymousType>type, mapper) : type;
59975998
}
59985999
if (type.flags & TypeFlags.Reference) {
@@ -6646,7 +6647,7 @@ namespace ts {
66466647
}
66476648

66486649
function hasExcessProperties(source: FreshObjectLiteralType, target: Type, reportErrors: boolean): boolean {
6649-
if (!(target.flags & TypeFlags.ObjectLiteralPatternWithComputedProperties) && maybeTypeOfKind(target, TypeFlags.ObjectType)) {
6650+
if (maybeTypeOfKind(target, TypeFlags.ObjectType) && !(target as ObjectType).inObjectLiteralPatternWithComputedProperties) {
66506651
for (const prop of getPropertiesOfObjectType(source)) {
66516652
if (!isKnownProperty(target, prop.name)) {
66526653
if (reportErrors) {
@@ -7140,12 +7141,12 @@ namespace ts {
71407141
// some level beyond that.
71417142
function isDeeplyNestedGeneric(type: Type, stack: Type[], depth: number): boolean {
71427143
// We track type references (created by createTypeReference) and instantiated types (created by instantiateType)
7143-
if (type.flags & (TypeFlags.Reference | TypeFlags.Instantiated) && depth >= 5) {
7144+
if ((type.flags & TypeFlags.Reference || type.flags & TypeFlags.Anonymous && (type as AnonymousType).isInstantiated) && depth >= 5) {
71447145
const symbol = type.symbol;
71457146
let count = 0;
71467147
for (let i = 0; i < depth; i++) {
71477148
const t = stack[i];
7148-
if (t.flags & (TypeFlags.Reference | TypeFlags.Instantiated) && t.symbol === symbol) {
7149+
if ((t.flags & TypeFlags.Reference || t.flags & TypeFlags.Anonymous && (t as AnonymousType).isInstantiated) && t.symbol === symbol) {
71497150
count++;
71507151
if (count >= 5) return true;
71517152
}
@@ -9937,7 +9938,7 @@ namespace ts {
99379938

99389939
// Return the contextual type for a given expression node. During overload resolution, a contextual type may temporarily
99399940
// be "pushed" onto a node using the contextualType property.
9940-
function getApparentTypeOfContextualType(node: Expression): Type {
9941+
function getApparentTypeOfContextualType(node: Expression): ObjectType {
99419942
const type = getContextualType(node);
99429943
return type && getApparentType(type);
99439944
}
@@ -10271,7 +10272,7 @@ namespace ts {
1027110272
const contextualTypeHasPattern = contextualType && contextualType.pattern &&
1027210273
(contextualType.pattern.kind === SyntaxKind.ObjectBindingPattern || contextualType.pattern.kind === SyntaxKind.ObjectLiteralExpression);
1027310274
let typeFlags: TypeFlags = 0;
10274-
let patternWithComputedProperties = false;
10275+
let patternWithComputedProperties: boolean | undefined;
1027510276
let hasComputedStringProperty = false;
1027610277
let hasComputedNumberProperty = false;
1027710278

@@ -10306,7 +10307,7 @@ namespace ts {
1030610307
patternWithComputedProperties = true;
1030710308
}
1030810309
}
10309-
else if (contextualTypeHasPattern && !(contextualType.flags & TypeFlags.ObjectLiteralPatternWithComputedProperties)) {
10310+
else if (contextualTypeHasPattern && !contextualType.inObjectLiteralPatternWithComputedProperties) {
1031010311
// If object literal is contextually typed by the implied type of a binding pattern, and if the
1031110312
// binding pattern specifies a default value for the property, make the property optional.
1031210313
const impliedProp = getPropertyOfType(contextualType, member.name);
@@ -10371,7 +10372,8 @@ namespace ts {
1037110372
const numberIndexInfo = hasComputedNumberProperty ? getObjectLiteralIndexInfo(node, propertiesArray, IndexKind.Number) : undefined;
1037210373
const result = createAnonymousType(node.symbol, propertiesTable, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo);
1037310374
const freshObjectLiteralFlag = compilerOptions.suppressExcessPropertyErrors ? 0 : TypeFlags.FreshLiteral;
10374-
result.flags |= TypeFlags.ObjectLiteral | TypeFlags.ContainsObjectLiteral | freshObjectLiteralFlag | (typeFlags & TypeFlags.PropagatingFlags) | (patternWithComputedProperties ? TypeFlags.ObjectLiteralPatternWithComputedProperties : 0);
10375+
result.flags |= TypeFlags.ObjectLiteral | TypeFlags.ContainsObjectLiteral | freshObjectLiteralFlag | (typeFlags & TypeFlags.PropagatingFlags);
10376+
result.inObjectLiteralPatternWithComputedProperties = patternWithComputedProperties;
1037510377
if (inDestructuringPattern) {
1037610378
result.pattern = node;
1037710379
}
@@ -10941,7 +10943,7 @@ namespace ts {
1094110943
return true;
1094210944
}
1094310945
// An instance property must be accessed through an instance of the enclosing class
10944-
if (type.flags & TypeFlags.ThisType) {
10946+
if (type.flags & TypeFlags.TypeParameter && (type as TypeParameter).isThisType) {
1094510947
// get the original type -- represented as the type constraint of the 'this' type
1094610948
type = getConstraintOfTypeParameter(<TypeParameter>type);
1094710949
}
@@ -10991,7 +10993,7 @@ namespace ts {
1099110993
const prop = getPropertyOfType(apparentType, right.text);
1099210994
if (!prop) {
1099310995
if (right.text && !checkAndReportErrorForExtendingInterface(node)) {
10994-
reportNonexistentProperty(right, type.flags & TypeFlags.ThisType ? apparentType : type);
10996+
reportNonexistentProperty(right, type.flags & TypeFlags.TypeParameter && (type as TypeParameter).isThisType ? apparentType : type);
1099510997
}
1099610998
return unknownType;
1099710999
}

src/compiler/types.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,19 +2382,16 @@ namespace ts {
23822382
Union = 1 << 19, // Union (T | U)
23832383
Intersection = 1 << 20, // Intersection (T & U)
23842384
Anonymous = 1 << 21, // Anonymous
2385-
Instantiated = 1 << 22, // Instantiated anonymous type
23862385
/* @internal */
2387-
ObjectLiteral = 1 << 23, // Originates in an object literal
2386+
ObjectLiteral = 1 << 22, // Originates in an object literal
23882387
/* @internal */
2389-
FreshLiteral = 1 << 24, // Fresh literal type
2388+
FreshLiteral = 1 << 23, // Fresh literal type
23902389
/* @internal */
2391-
ContainsWideningType = 1 << 25, // Type is or contains undefined or null widening type
2390+
ContainsWideningType = 1 << 24, // Type is or contains undefined or null widening type
23922391
/* @internal */
2393-
ContainsObjectLiteral = 1 << 26, // Type is or contains object literal type
2392+
ContainsObjectLiteral = 1 << 25, // Type is or contains object literal type
23942393
/* @internal */
2395-
ContainsAnyFunctionType = 1 << 27, // Type is or contains object literal type
2396-
ThisType = 1 << 28, // This type
2397-
ObjectLiteralPatternWithComputedProperties = 1 << 29, // Object literal type implied by binding pattern has computed properties
2394+
ContainsAnyFunctionType = 1 << 26, // Type is or contains object literal type
23982395

23992396
/* @internal */
24002397
Nullable = Undefined | Null,
@@ -2463,7 +2460,9 @@ namespace ts {
24632460
}
24642461

24652462
// Object types (TypeFlags.ObjectType)
2466-
export interface ObjectType extends Type { }
2463+
export interface ObjectType extends Type {
2464+
inObjectLiteralPatternWithComputedProperties?: boolean;
2465+
}
24672466

24682467
// Class and interface types (TypeFlags.Class and TypeFlags.Interface)
24692468
export interface InterfaceType extends ObjectType {
@@ -2519,6 +2518,7 @@ namespace ts {
25192518
/* @internal */
25202519
// An instantiated anonymous type has a target and a mapper
25212520
export interface AnonymousType extends ObjectType {
2521+
isInstantiated?: boolean;
25222522
target?: AnonymousType; // Instantiation target
25232523
mapper?: TypeMapper; // Instantiation mapper
25242524
}
@@ -2558,6 +2558,8 @@ namespace ts {
25582558
mapper?: TypeMapper; // Instantiation mapper
25592559
/* @internal */
25602560
resolvedApparentType: Type;
2561+
/* @internal */
2562+
isThisType?: boolean;
25612563
}
25622564

25632565
export const enum SignatureKind {

0 commit comments

Comments
 (0)