Skip to content

Commit 045b51a

Browse files
committed
Use {} type facts for unconstrained type params
Previously it was using TypeFacts.All. But the constraint of an unconstrained type parameter is actually {}.
1 parent 4189b4d commit 045b51a

File tree

2 files changed

+10
-25
lines changed

2 files changed

+10
-25
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,7 @@ namespace ts {
11871187
}
11881188

11891189
function resolveSymbol(symbol: Symbol): Symbol {
1190-
return symbol && symbol.flags & SymbolFlags.Alias && !(symbol.flags & (SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace)) ? resolveAlias(symbol) : symbol;
1190+
return symbol && symbol.flags & SymbolFlags.Alias && !(symbol.flags & (SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace)) ? resolveAlias(symbol) : symbol;
11911191
}
11921192

11931193
function resolveAlias(symbol: Symbol): Symbol {
@@ -7892,25 +7892,21 @@ namespace ts {
78927892
}
78937893
if (flags & TypeFlags.TypeParameter) {
78947894
const constraint = getConstraintOfTypeParameter(<TypeParameter>type);
7895-
return constraint ? getTypeFacts(constraint) : TypeFacts.All;
7895+
return getTypeFacts(constraint || emptyObjectType);
78967896
}
78977897
if (flags & TypeFlags.UnionOrIntersection) {
78987898
return getTypeFactsOfTypes((<UnionOrIntersectionType>type).types);
78997899
}
79007900
return TypeFacts.All;
79017901
}
79027902

7903-
function getTypeWithFacts(type: Type, include: TypeFacts, intersectForTypeParameters = false) {
7903+
function getTypeWithFacts(type: Type, include: TypeFacts) {
79047904
if (!(type.flags & TypeFlags.Union)) {
79057905
return getTypeFacts(type) & include ? type : neverType;
79067906
}
79077907
let firstType: Type;
7908-
let hasTypeParameter = false;
79097908
let types: Type[];
79107909
for (const t of (type as UnionType).types) {
7911-
if (t.flags & TypeFlags.TypeParameter) {
7912-
hasTypeParameter = true;
7913-
}
79147910
if (getTypeFacts(t) & include) {
79157911
if (!firstType) {
79167912
firstType = t;
@@ -7923,19 +7919,8 @@ namespace ts {
79237919
}
79247920
}
79257921
}
7926-
const narrowed = types ? getUnionType(types) :
7927-
firstType ? firstType : neverType;
7928-
// if there is a type parameter in the narrowed type,
7929-
// add an intersection with the members of the narrowed type so that the shape of the type is correct
7930-
if (type.flags & TypeFlags.Union &&
7931-
narrowed.flags & TypeFlags.Union &&
7932-
hasTypeParameter &&
7933-
intersectForTypeParameters) {
7934-
return getIntersectionType(types.concat([narrowed]));
7935-
}
7936-
else {
7937-
return narrowed;
7938-
}
7922+
return types ? getUnionType(types) :
7923+
firstType ? firstType : neverType;
79397924
}
79407925

79417926
function getTypeWithDefault(type: Type, defaultExpression: Expression) {
@@ -8308,10 +8293,10 @@ namespace ts {
83088293

83098294
function narrowTypeByTruthiness(type: Type, expr: Expression, assumeTrue: boolean): Type {
83108295
if (isMatchingReference(reference, expr)) {
8311-
return getTypeWithFacts(type, assumeTrue ? TypeFacts.Truthy : TypeFacts.Falsy, assumeTrue);
8296+
return getTypeWithFacts(type, assumeTrue ? TypeFacts.Truthy : TypeFacts.Falsy);
83128297
}
83138298
if (isMatchingPropertyAccess(expr)) {
8314-
return narrowTypeByDiscriminant(type, <PropertyAccessExpression>expr, t => getTypeWithFacts(t, assumeTrue ? TypeFacts.Truthy : TypeFacts.Falsy, assumeTrue));
8299+
return narrowTypeByDiscriminant(type, <PropertyAccessExpression>expr, t => getTypeWithFacts(t, assumeTrue ? TypeFacts.Truthy : TypeFacts.Falsy));
83158300
}
83168301
return type;
83178302
}
@@ -8369,7 +8354,7 @@ namespace ts {
83698354
value.kind === SyntaxKind.NullKeyword ?
83708355
assumeTrue ? TypeFacts.EQNull : TypeFacts.NENull :
83718356
assumeTrue ? TypeFacts.EQUndefined : TypeFacts.NEUndefined;
8372-
return getTypeWithFacts(type, facts, assumeTrue);
8357+
return getTypeWithFacts(type, facts);
83738358
}
83748359
if (type.flags & TypeFlags.NotUnionOrUnit) {
83758360
return type;
@@ -8407,7 +8392,7 @@ namespace ts {
84078392
const facts = assumeTrue ?
84088393
getProperty(typeofEQFacts, literal.text) || TypeFacts.TypeofEQHostObject :
84098394
getProperty(typeofNEFacts, literal.text) || TypeFacts.TypeofNEHostObject;
8410-
return getTypeWithFacts(type, facts, assumeTrue);
8395+
return getTypeWithFacts(type, facts);
84118396
}
84128397

84138398
function narrowTypeBySwitchOnDiscriminant(type: Type, switchStatement: SwitchStatement, clauseStart: number, clauseEnd: number) {

tests/baselines/reference/controlFlowIfStatement.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function c<T>(data: string | T): T {
108108
>JSON.parse : (text: string, reviver?: (key: any, value: any) => any) => any
109109
>JSON : JSON
110110
>parse : (text: string, reviver?: (key: any, value: any) => any) => any
111-
>data : string & T & (string | T)
111+
>data : string
112112
}
113113
else {
114114
return data;

0 commit comments

Comments
 (0)