Skip to content
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

Make enum literal type identical to the union of its values #60417

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15146,7 +15146,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
* no constituent property has type 'never', but the intersection of the constituent property types is 'never'.
*/
function getReducedType(type: Type): Type {
if (type.flags & TypeFlags.Union && (type as UnionType).objectFlags & ObjectFlags.ContainsIntersections) {
if (type.flags & TypeFlags.Union && (type as UnionType).objectFlags) {
return (type as UnionType).resolvedReducedType || ((type as UnionType).resolvedReducedType = getReducedUnionType(type as UnionType));
}
else if (type.flags & TypeFlags.Intersection) {
Expand All @@ -15161,14 +15161,32 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

function getReducedUnionType(unionType: UnionType) {
const reducedTypes = sameMap(unionType.types, getReducedType);
if (reducedTypes === unionType.types) {
return unionType;

if (reducedTypes !== unionType.types) {
const reduced = getUnionType(reducedTypes);
if (reduced.flags & TypeFlags.Union) {
unionType = reduced as UnionType;
}
else {
return reduced;
}
}
const reduced = getUnionType(reducedTypes);
if (reduced.flags & TypeFlags.Union) {
(reduced as UnionType).resolvedReducedType = reduced;

let reducedType: Type = unionType;

if (!(unionType.flags & TypeFlags.EnumLiteral) && every(unionType.types, t => !!(t.flags & TypeFlags.EnumLike))) {
const baseType = getBaseTypeOfEnumLikeType(first(unionType.types));
if (
baseType.flags & TypeFlags.Union &&
unionType.types.length === (baseType as UnionType).types.length &&
every(unionType.types, t => getBaseTypeOfEnumLikeType(t) === baseType)
) {
reducedType = baseType;
}
}
return reduced;

unionType.resolvedReducedType = reducedType;
return reducedType;
}

function isNeverReducedProperty(prop: Symbol) {
Expand Down
40 changes: 20 additions & 20 deletions tests/baselines/reference/enumLiteralTypes1.types
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type NoYes = Choice.No | Choice.Yes;
> : ^^^

type UnknownYesNo = Choice.Unknown | Choice.Yes | Choice.No;
>UnknownYesNo : UnknownYesNo
> : ^^^^^^^^^^^^
>UnknownYesNo : Choice
> : ^^^^^^
>Choice : any
> : ^^^
>Choice : any
Expand Down Expand Up @@ -71,16 +71,16 @@ function f2(a: YesNo, b: UnknownYesNo, c: Choice) {
> : ^ ^^ ^^ ^^ ^^ ^^ ^^^^^^^^^
>a : YesNo
> : ^^^^^
>b : UnknownYesNo
> : ^^^^^^^^^^^^
>b : Choice
> : ^^^^^^
>c : Choice
> : ^^^^^^

b = a;
>b = a : YesNo
> : ^^^^^
>b : UnknownYesNo
> : ^^^^^^^^^^^^
>b : Choice
> : ^^^^^^
>a : YesNo
> : ^^^^^

Expand Down Expand Up @@ -346,8 +346,8 @@ function f5(a: YesNo, b: UnknownYesNo, c: Choice) {
> : ^ ^^ ^^ ^^ ^^ ^^ ^^^^^^^^^
>a : YesNo
> : ^^^^^
>b : UnknownYesNo
> : ^^^^^^^^^^^^
>b : Choice
> : ^^^^^^
>c : Choice
> : ^^^^^^

Expand Down Expand Up @@ -396,8 +396,8 @@ function f5(a: YesNo, b: UnknownYesNo, c: Choice) {
> : ^^^^^^
>g : { (x: Choice.Yes): string; (x: Choice.No): boolean; (x: Choice): number; }
> : ^^^ ^^ ^^^ ^^^ ^^ ^^^ ^^^ ^^ ^^^ ^^^
>b : UnknownYesNo
> : ^^^^^^^^^^^^
>b : Choice
> : ^^^^^^

var z5 = g(c);
>z5 : number
Expand Down Expand Up @@ -499,35 +499,35 @@ function f11(x: YesNo) {
function f12(x: UnknownYesNo) {
>f12 : (x: UnknownYesNo) => void
> : ^ ^^ ^^^^^^^^^
>x : UnknownYesNo
> : ^^^^^^^^^^^^
>x : Choice
> : ^^^^^^

if (x) {
>x : UnknownYesNo
> : ^^^^^^^^^^^^
>x : Choice
> : ^^^^^^

x;
>x : Choice.Yes | Choice.No
> : ^^^^^^^^^^^^^^^^^^^^^^
}
else {
x;
>x : UnknownYesNo
> : ^^^^^^^^^^^^
>x : Choice
> : ^^^^^^
}
}

function f13(x: UnknownYesNo) {
>f13 : (x: UnknownYesNo) => void
> : ^ ^^ ^^^^^^^^^
>x : UnknownYesNo
> : ^^^^^^^^^^^^
>x : Choice
> : ^^^^^^

if (x === Choice.Yes) {
>x === Choice.Yes : boolean
> : ^^^^^^^
>x : UnknownYesNo
> : ^^^^^^^^^^^^
>x : Choice
> : ^^^^^^
>Choice.Yes : Choice.Yes
> : ^^^^^^^^^^
>Choice : typeof Choice
Expand Down
Loading