Skip to content

Commit c110f57

Browse files
committed
Discriminant must include at least one unit type and no instantiable types
1 parent d069875 commit c110f57

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/compiler/checker.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12919,10 +12919,6 @@ namespace ts {
1291912919
isUnitType(type);
1292012920
}
1292112921

12922-
function maybeUnitType(type: Type): boolean {
12923-
return type.flags & TypeFlags.Union ? some((<UnionType>type).types, isUnitType) : isUnitType(type);
12924-
}
12925-
1292612922
function getBaseTypeOfLiteralType(type: Type): Type {
1292712923
return type.flags & TypeFlags.EnumLiteral ? getBaseTypeOfEnumLiteralType(<LiteralType>type) :
1292812924
type.flags & TypeFlags.StringLiteral ? stringType :
@@ -14216,12 +14212,26 @@ namespace ts {
1421614212
return undefined;
1421714213
}
1421814214

14215+
function isDiscriminantType(type: Type): boolean {
14216+
if (type.flags & TypeFlags.Union) {
14217+
if (type.flags & (TypeFlags.Boolean | TypeFlags.EnumLiteral)) {
14218+
return true;
14219+
}
14220+
let combined = 0;
14221+
for (const t of (<UnionType>type).types) combined |= t.flags;
14222+
if (combined & TypeFlags.Unit && !(combined & TypeFlags.Instantiable)) {
14223+
return true;
14224+
}
14225+
}
14226+
return false;
14227+
}
14228+
1421914229
function isDiscriminantProperty(type: Type | undefined, name: __String) {
1422014230
if (type && type.flags & TypeFlags.Union) {
1422114231
const prop = getUnionOrIntersectionProperty(<UnionType>type, name);
1422214232
if (prop && getCheckFlags(prop) & CheckFlags.SyntheticProperty) {
1422314233
if ((<TransientSymbol>prop).isDiscriminantProperty === undefined) {
14224-
(<TransientSymbol>prop).isDiscriminantProperty = !!((<TransientSymbol>prop).checkFlags & CheckFlags.HasNonUniformType) && maybeUnitType(getTypeOfSymbol(prop));
14234+
(<TransientSymbol>prop).isDiscriminantProperty = !!((<TransientSymbol>prop).checkFlags & CheckFlags.HasNonUniformType) && isDiscriminantType(getTypeOfSymbol(prop));
1422514235
}
1422614236
return !!(<TransientSymbol>prop).isDiscriminantProperty;
1422714237
}

0 commit comments

Comments
 (0)