@@ -157,6 +157,7 @@ import {
157
157
entityNameToString,
158
158
EnumDeclaration,
159
159
EnumMember,
160
+ EnumType,
160
161
equateValues,
161
162
escapeLeadingUnderscores,
162
163
escapeString,
@@ -9660,8 +9661,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
9660
9661
const t = types[i];
9661
9662
flags |= t.flags;
9662
9663
if (!(t.flags & TypeFlags.Nullable)) {
9663
- if (t.flags & (TypeFlags.BooleanLiteral | TypeFlags.EnumLiteral )) {
9664
- const baseType = t.flags & TypeFlags.BooleanLiteral ? booleanType : getBaseTypeOfEnumLiteralType (t as LiteralType);
9664
+ if (t.flags & (TypeFlags.BooleanLiteral | TypeFlags.EnumLike )) {
9665
+ const baseType = t.flags & TypeFlags.BooleanLiteral ? booleanType : getBaseTypeOfEnumLikeType (t as LiteralType);
9665
9666
if (baseType.flags & TypeFlags.Union) {
9666
9667
const count = (baseType as UnionType).types.length;
9667
9668
if (i + count <= types.length && getRegularTypeOfLiteralType(types[i + count - 1]) === getRegularTypeOfLiteralType((baseType as UnionType).types[count - 1])) {
@@ -11906,8 +11907,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
11906
11907
return links.declaredType;
11907
11908
}
11908
11909
11909
- function getBaseTypeOfEnumLiteralType (type: Type) {
11910
- return type.flags & TypeFlags.EnumLiteral && !( type.flags & TypeFlags.Union) ? getDeclaredTypeOfSymbol(getParentOfSymbol(type.symbol)!) : type;
11910
+ function getBaseTypeOfEnumLikeType (type: Type) {
11911
+ return type.flags & TypeFlags.EnumLike && type.symbol. flags & SymbolFlags.EnumMember ? getDeclaredTypeOfSymbol(getParentOfSymbol(type.symbol)!) : type;
11911
11912
}
11912
11913
11913
11914
function getDeclaredTypeOfEnum(symbol: Symbol): Type {
@@ -11921,9 +11922,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
11921
11922
if (hasBindableName(member)) {
11922
11923
const memberSymbol = getSymbolOfDeclaration(member);
11923
11924
const value = getEnumMemberValue(member);
11924
- const memberType = value !== undefined ?
11925
- getFreshTypeOfLiteralType( getEnumLiteralType(value, getSymbolId(symbol), memberSymbol) ) :
11926
- createTypeWithSymbol(TypeFlags.Enum, memberSymbol);
11925
+ const memberType = getFreshTypeOfLiteralType( value !== undefined ?
11926
+ getEnumLiteralType(value, getSymbolId(symbol), memberSymbol) :
11927
+ createComputedEnumType( memberSymbol) );
11927
11928
getSymbolLinks(memberSymbol).declaredType = memberType;
11928
11929
memberTypeList.push(getRegularTypeOfLiteralType(memberType));
11929
11930
}
@@ -11933,7 +11934,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
11933
11934
}
11934
11935
const enumType = memberTypeList.length ?
11935
11936
getUnionType(memberTypeList, UnionReduction.Literal, symbol, /*aliasTypeArguments*/ undefined) :
11936
- createTypeWithSymbol(TypeFlags.Enum, symbol);
11937
+ createComputedEnumType( symbol);
11937
11938
if (enumType.flags & TypeFlags.Union) {
11938
11939
enumType.flags |= TypeFlags.EnumLiteral;
11939
11940
enumType.symbol = symbol;
@@ -11943,6 +11944,16 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
11943
11944
return links.declaredType;
11944
11945
}
11945
11946
11947
+ function createComputedEnumType(symbol: Symbol) {
11948
+ const regularType = createTypeWithSymbol(TypeFlags.Enum, symbol) as EnumType;
11949
+ const freshType = createTypeWithSymbol(TypeFlags.Enum, symbol) as EnumType;
11950
+ regularType.regularType = regularType;
11951
+ regularType.freshType = freshType;
11952
+ freshType.regularType = regularType;
11953
+ freshType.freshType = freshType;
11954
+ return regularType;
11955
+ }
11956
+
11946
11957
function getDeclaredTypeOfEnumMember(symbol: Symbol): Type {
11947
11958
const links = getSymbolLinks(symbol);
11948
11959
if (!links.declaredType) {
@@ -16201,7 +16212,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
16201
16212
orderedRemoveItemAt(typeSet, 1);
16202
16213
}
16203
16214
}
16204
- if (includes & (TypeFlags.Literal | TypeFlags.UniqueESSymbol | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) || includes & TypeFlags.Void && includes & TypeFlags.Undefined) {
16215
+ if (includes & (TypeFlags.Enum | TypeFlags. Literal | TypeFlags.UniqueESSymbol | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) || includes & TypeFlags.Void && includes & TypeFlags.Undefined) {
16205
16216
removeRedundantLiteralTypes(typeSet, includes, !!(unionReduction & UnionReduction.Subtype));
16206
16217
}
16207
16218
if (includes & TypeFlags.StringLiteral && includes & TypeFlags.TemplateLiteral) {
@@ -18046,25 +18057,25 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
18046
18057
}
18047
18058
18048
18059
function getFreshTypeOfLiteralType(type: Type): Type {
18049
- if (type.flags & TypeFlags.Literal ) {
18050
- if (!(type as LiteralType ).freshType) {
18060
+ if (type.flags & TypeFlags.Freshable ) {
18061
+ if (!(type as FreshableType ).freshType) {
18051
18062
const freshType = createLiteralType(type.flags, (type as LiteralType).value, (type as LiteralType).symbol, type as LiteralType);
18052
18063
freshType.freshType = freshType;
18053
- (type as LiteralType ).freshType = freshType;
18064
+ (type as FreshableType ).freshType = freshType;
18054
18065
}
18055
- return (type as LiteralType ).freshType;
18066
+ return (type as FreshableType ).freshType;
18056
18067
}
18057
18068
return type;
18058
18069
}
18059
18070
18060
18071
function getRegularTypeOfLiteralType(type: Type): Type {
18061
- return type.flags & TypeFlags.Literal ? (type as LiteralType ).regularType :
18072
+ return type.flags & TypeFlags.Freshable ? (type as FreshableType ).regularType :
18062
18073
type.flags & TypeFlags.Union ? ((type as UnionType).regularType || ((type as UnionType).regularType = mapType(type, getRegularTypeOfLiteralType) as UnionType)) :
18063
18074
type;
18064
18075
}
18065
18076
18066
18077
function isFreshLiteralType(type: Type) {
18067
- return !!(type.flags & TypeFlags.Literal ) && (type as LiteralType).freshType === type;
18078
+ return !!(type.flags & TypeFlags.Freshable ) && (type as LiteralType).freshType === type;
18068
18079
}
18069
18080
18070
18081
function getStringLiteralType(value: string): StringLiteralType {
@@ -22993,7 +23004,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
22993
23004
}
22994
23005
22995
23006
function getBaseTypeOfLiteralType(type: Type): Type {
22996
- return type.flags & TypeFlags.EnumLiteral ? getBaseTypeOfEnumLiteralType (type as LiteralType) :
23007
+ return type.flags & TypeFlags.EnumLike ? getBaseTypeOfEnumLikeType (type as LiteralType) :
22997
23008
type.flags & (TypeFlags.StringLiteral | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) ? stringType :
22998
23009
type.flags & TypeFlags.NumberLiteral ? numberType :
22999
23010
type.flags & TypeFlags.BigIntLiteral ? bigintType :
@@ -23008,7 +23019,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
23008
23019
}
23009
23020
23010
23021
function getWidenedLiteralType(type: Type): Type {
23011
- return type.flags & TypeFlags.EnumLiteral && isFreshLiteralType(type) ? getBaseTypeOfEnumLiteralType (type as LiteralType) :
23022
+ return type.flags & TypeFlags.EnumLike && isFreshLiteralType(type) ? getBaseTypeOfEnumLikeType (type as LiteralType) :
23012
23023
type.flags & TypeFlags.StringLiteral && isFreshLiteralType(type) ? stringType :
23013
23024
type.flags & TypeFlags.NumberLiteral && isFreshLiteralType(type) ? numberType :
23014
23025
type.flags & TypeFlags.BigIntLiteral && isFreshLiteralType(type) ? bigintType :
@@ -25652,7 +25663,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
25652
25663
}
25653
25664
return true;
25654
25665
}
25655
- if (source.flags & TypeFlags.EnumLiteral && getBaseTypeOfEnumLiteralType (source as LiteralType) === target) {
25666
+ if (source.flags & TypeFlags.EnumLike && getBaseTypeOfEnumLikeType (source as LiteralType) === target) {
25656
25667
return true;
25657
25668
}
25658
25669
return containsType(target.types, source);
@@ -45995,7 +46006,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
45995
46006
}
45996
46007
45997
46008
function literalTypeToNode(type: FreshableType, enclosing: Node, tracker: SymbolTracker): Expression {
45998
- const enumResult = type.flags & TypeFlags.EnumLiteral ? nodeBuilder.symbolToExpression(type.symbol, SymbolFlags.Value, enclosing, /*flags*/ undefined, tracker)
46009
+ const enumResult = type.flags & TypeFlags.EnumLike ? nodeBuilder.symbolToExpression(type.symbol, SymbolFlags.Value, enclosing, /*flags*/ undefined, tracker)
45999
46010
: type === trueType ? factory.createTrue() : type === falseType && factory.createFalse();
46000
46011
if (enumResult) return enumResult;
46001
46012
const literalValue = (type as LiteralType).value;
@@ -47782,7 +47793,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
47782
47793
function isSimpleLiteralEnumReference(expr: Expression) {
47783
47794
if ((isPropertyAccessExpression(expr) || (isElementAccessExpression(expr) && isStringOrNumberLiteralExpression(expr.argumentExpression))) &&
47784
47795
isEntityNameExpression(expr.expression)) {
47785
- return !!(checkExpressionCached(expr).flags & TypeFlags.EnumLiteral );
47796
+ return !!(checkExpressionCached(expr).flags & TypeFlags.EnumLike );
47786
47797
}
47787
47798
}
47788
47799
0 commit comments