-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Prefer using enum literal's own base type rather than enum's base type in comparison #52703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22983,6 +22983,23 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
return getCachedType(key) ?? setCachedType(key, mapType(type, getBaseTypeOfLiteralType)); | ||
} | ||
|
||
// This is the same as getBaseTypeOfLiteralType, but checks EnumLiteral last so we get | ||
// a specific literal's base type rather than the enum's base type. | ||
function getBaseTypeOfLiteralTypeForComparison(type: Type): Type { | ||
return type.flags & (TypeFlags.StringLiteral | TypeFlags.TemplateLiteral | TypeFlags.StringMapping) ? stringType : | ||
type.flags & TypeFlags.NumberLiteral ? numberType : | ||
type.flags & TypeFlags.BigIntLiteral ? bigintType : | ||
type.flags & TypeFlags.BooleanLiteral ? booleanType : | ||
type.flags & TypeFlags.EnumLiteral ? getBaseTypeOfEnumLiteralType(type as LiteralType) : | ||
type.flags & TypeFlags.Union ? getBaseTypeOfLiteralTypeUnionForComparison(type as UnionType) : | ||
type; | ||
} | ||
|
||
function getBaseTypeOfLiteralTypeUnionForComparison(type: UnionType) { | ||
const key = `BC${getTypeId(type)}`; | ||
return getCachedType(key) ?? setCachedType(key, mapType(type, getBaseTypeOfLiteralTypeForComparison)); | ||
} | ||
|
||
function getWidenedLiteralType(type: Type): Type { | ||
return type.flags & TypeFlags.EnumLiteral && isFreshLiteralType(type) ? getBaseTypeOfEnumLiteralType(type as LiteralType) : | ||
type.flags & TypeFlags.StringLiteral && isFreshLiteralType(type) ? stringType : | ||
|
@@ -36423,8 +36440,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { | |
case SyntaxKind.LessThanEqualsToken: | ||
case SyntaxKind.GreaterThanEqualsToken: | ||
if (checkForDisallowedESSymbolOperand(operator)) { | ||
leftType = getBaseTypeOfLiteralType(checkNonNullType(leftType, left)); | ||
rightType = getBaseTypeOfLiteralType(checkNonNullType(rightType, right)); | ||
leftType = getBaseTypeOfLiteralTypeForComparison(checkNonNullType(leftType, left)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect this isn't the only case; the
Because the result is actually used for the output, whereas this comparison block always gives back bool and so the unions would not be visible. |
||
rightType = getBaseTypeOfLiteralTypeForComparison(checkNonNullType(rightType, right)); | ||
reportOperatorErrorUnless((left, right) => { | ||
if (isTypeAny(left) || isTypeAny(right)) { | ||
return true; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//// [mixedTypeEnumComparison.ts] | ||
const enum E { | ||
S1 = "foo", | ||
S2 = "bar", | ||
|
||
N1 = 1000, | ||
N2 = 25, | ||
} | ||
|
||
declare var someNumber: number | ||
|
||
if (someNumber > E.N2) { | ||
someNumber = E.N2; | ||
} | ||
|
||
declare const unionOfEnum: E.N1 | E.N2; | ||
|
||
if (someNumber > unionOfEnum) { | ||
someNumber = E.N2; | ||
} | ||
|
||
declare var someString: string | ||
|
||
if (someString > E.S1) { | ||
someString = E.S2; | ||
} | ||
|
||
|
||
|
||
//// [mixedTypeEnumComparison.js] | ||
"use strict"; | ||
if (someNumber > 25 /* E.N2 */) { | ||
someNumber = 25 /* E.N2 */; | ||
} | ||
if (someNumber > unionOfEnum) { | ||
someNumber = 25 /* E.N2 */; | ||
} | ||
if (someString > "foo" /* E.S1 */) { | ||
someString = "bar" /* E.S2 */; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
=== tests/cases/compiler/mixedTypeEnumComparison.ts === | ||
const enum E { | ||
>E : Symbol(E, Decl(mixedTypeEnumComparison.ts, 0, 0)) | ||
|
||
S1 = "foo", | ||
>S1 : Symbol(E.S1, Decl(mixedTypeEnumComparison.ts, 0, 14)) | ||
|
||
S2 = "bar", | ||
>S2 : Symbol(E.S2, Decl(mixedTypeEnumComparison.ts, 1, 15)) | ||
|
||
N1 = 1000, | ||
>N1 : Symbol(E.N1, Decl(mixedTypeEnumComparison.ts, 2, 15)) | ||
|
||
N2 = 25, | ||
>N2 : Symbol(E.N2, Decl(mixedTypeEnumComparison.ts, 4, 14)) | ||
} | ||
|
||
declare var someNumber: number | ||
>someNumber : Symbol(someNumber, Decl(mixedTypeEnumComparison.ts, 8, 11)) | ||
|
||
if (someNumber > E.N2) { | ||
>someNumber : Symbol(someNumber, Decl(mixedTypeEnumComparison.ts, 8, 11)) | ||
>E.N2 : Symbol(E.N2, Decl(mixedTypeEnumComparison.ts, 4, 14)) | ||
>E : Symbol(E, Decl(mixedTypeEnumComparison.ts, 0, 0)) | ||
>N2 : Symbol(E.N2, Decl(mixedTypeEnumComparison.ts, 4, 14)) | ||
|
||
someNumber = E.N2; | ||
>someNumber : Symbol(someNumber, Decl(mixedTypeEnumComparison.ts, 8, 11)) | ||
>E.N2 : Symbol(E.N2, Decl(mixedTypeEnumComparison.ts, 4, 14)) | ||
>E : Symbol(E, Decl(mixedTypeEnumComparison.ts, 0, 0)) | ||
>N2 : Symbol(E.N2, Decl(mixedTypeEnumComparison.ts, 4, 14)) | ||
} | ||
|
||
declare const unionOfEnum: E.N1 | E.N2; | ||
>unionOfEnum : Symbol(unionOfEnum, Decl(mixedTypeEnumComparison.ts, 14, 13)) | ||
>E : Symbol(E, Decl(mixedTypeEnumComparison.ts, 0, 0)) | ||
>N1 : Symbol(E.N1, Decl(mixedTypeEnumComparison.ts, 2, 15)) | ||
>E : Symbol(E, Decl(mixedTypeEnumComparison.ts, 0, 0)) | ||
>N2 : Symbol(E.N2, Decl(mixedTypeEnumComparison.ts, 4, 14)) | ||
|
||
if (someNumber > unionOfEnum) { | ||
>someNumber : Symbol(someNumber, Decl(mixedTypeEnumComparison.ts, 8, 11)) | ||
>unionOfEnum : Symbol(unionOfEnum, Decl(mixedTypeEnumComparison.ts, 14, 13)) | ||
|
||
someNumber = E.N2; | ||
>someNumber : Symbol(someNumber, Decl(mixedTypeEnumComparison.ts, 8, 11)) | ||
>E.N2 : Symbol(E.N2, Decl(mixedTypeEnumComparison.ts, 4, 14)) | ||
>E : Symbol(E, Decl(mixedTypeEnumComparison.ts, 0, 0)) | ||
>N2 : Symbol(E.N2, Decl(mixedTypeEnumComparison.ts, 4, 14)) | ||
} | ||
|
||
declare var someString: string | ||
>someString : Symbol(someString, Decl(mixedTypeEnumComparison.ts, 20, 11)) | ||
|
||
if (someString > E.S1) { | ||
>someString : Symbol(someString, Decl(mixedTypeEnumComparison.ts, 20, 11)) | ||
>E.S1 : Symbol(E.S1, Decl(mixedTypeEnumComparison.ts, 0, 14)) | ||
>E : Symbol(E, Decl(mixedTypeEnumComparison.ts, 0, 0)) | ||
>S1 : Symbol(E.S1, Decl(mixedTypeEnumComparison.ts, 0, 14)) | ||
|
||
someString = E.S2; | ||
>someString : Symbol(someString, Decl(mixedTypeEnumComparison.ts, 20, 11)) | ||
>E.S2 : Symbol(E.S2, Decl(mixedTypeEnumComparison.ts, 1, 15)) | ||
>E : Symbol(E, Decl(mixedTypeEnumComparison.ts, 0, 0)) | ||
>S2 : Symbol(E.S2, Decl(mixedTypeEnumComparison.ts, 1, 15)) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
=== tests/cases/compiler/mixedTypeEnumComparison.ts === | ||
const enum E { | ||
>E : E | ||
|
||
S1 = "foo", | ||
>S1 : E.S1 | ||
>"foo" : "foo" | ||
|
||
S2 = "bar", | ||
>S2 : E.S2 | ||
>"bar" : "bar" | ||
|
||
N1 = 1000, | ||
>N1 : E.N1 | ||
>1000 : 1000 | ||
|
||
N2 = 25, | ||
>N2 : E.N2 | ||
>25 : 25 | ||
} | ||
|
||
declare var someNumber: number | ||
>someNumber : number | ||
|
||
if (someNumber > E.N2) { | ||
>someNumber > E.N2 : boolean | ||
>someNumber : number | ||
>E.N2 : E.N2 | ||
>E : typeof E | ||
>N2 : E.N2 | ||
|
||
someNumber = E.N2; | ||
>someNumber = E.N2 : E.N2 | ||
>someNumber : number | ||
>E.N2 : E.N2 | ||
>E : typeof E | ||
>N2 : E.N2 | ||
} | ||
|
||
declare const unionOfEnum: E.N1 | E.N2; | ||
>unionOfEnum : E.N1 | E.N2 | ||
>E : any | ||
>E : any | ||
|
||
if (someNumber > unionOfEnum) { | ||
>someNumber > unionOfEnum : boolean | ||
>someNumber : number | ||
>unionOfEnum : E.N1 | E.N2 | ||
|
||
someNumber = E.N2; | ||
>someNumber = E.N2 : E.N2 | ||
>someNumber : number | ||
>E.N2 : E.N2 | ||
>E : typeof E | ||
>N2 : E.N2 | ||
} | ||
|
||
declare var someString: string | ||
>someString : string | ||
|
||
if (someString > E.S1) { | ||
>someString > E.S1 : boolean | ||
>someString : string | ||
>E.S1 : E.S1 | ||
>E : typeof E | ||
>S1 : E.S1 | ||
|
||
someString = E.S2; | ||
>someString = E.S2 : E.S2 | ||
>someString : string | ||
>E.S2 : E.S2 | ||
>E : typeof E | ||
>S2 : E.S2 | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// @strict: true | ||
|
||
const enum E { | ||
S1 = "foo", | ||
S2 = "bar", | ||
|
||
N1 = 1000, | ||
N2 = 25, | ||
} | ||
|
||
declare var someNumber: number | ||
|
||
if (someNumber > E.N2) { | ||
someNumber = E.N2; | ||
} | ||
|
||
declare const unionOfEnum: E.N1 | E.N2; | ||
|
||
if (someNumber > unionOfEnum) { | ||
someNumber = E.N2; | ||
} | ||
|
||
declare var someString: string | ||
|
||
if (someString > E.S1) { | ||
someString = E.S2; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.