Skip to content

Commit 9aec49b

Browse files
committed
Nullable errors on unconstrained generic types in strictNullChecks mode
1 parent b9b07c4 commit 9aec49b

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

src/compiler/checker.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,8 +1226,11 @@ export const enum TypeFacts {
12261226
Falsy = 1 << 23, // !x
12271227
IsUndefined = 1 << 24, // Contains undefined or intersection with undefined
12281228
IsNull = 1 << 25, // Contains null or intersection with null
1229+
IsPossiblyUndefined = 1 << 26, // Possibly undefined
1230+
IsPossiblyNull = 1 << 27, // Possibly null
12291231
IsUndefinedOrNull = IsUndefined | IsNull,
1230-
All = (1 << 27) - 1,
1232+
IsPossiblyUndefinedOrNull = IsPossiblyUndefined | IsPossiblyNull,
1233+
All = (1 << 29) - 1,
12311234
// The following members encode facts about particular kinds of types for use in the getTypeFacts function.
12321235
// The presence of a particular fact means that the given test is true for some (and possibly all) values
12331236
// of that kind of type.
@@ -1270,11 +1273,12 @@ export const enum TypeFacts {
12701273
FunctionStrictFacts = TypeofEQFunction | TypeofEQHostObject | TypeofNEString | TypeofNENumber | TypeofNEBigInt | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | NEUndefined | NENull | NEUndefinedOrNull | Truthy,
12711274
FunctionFacts = FunctionStrictFacts | EQUndefined | EQNull | EQUndefinedOrNull | Falsy,
12721275
VoidFacts = TypeofNEString | TypeofNENumber | TypeofNEBigInt | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | TypeofNEFunction | TypeofNEHostObject | EQUndefined | EQUndefinedOrNull | NENull | Falsy,
1273-
UndefinedFacts = TypeofNEString | TypeofNENumber | TypeofNEBigInt | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | TypeofNEFunction | TypeofNEHostObject | EQUndefined | EQUndefinedOrNull | NENull | Falsy | IsUndefined,
1274-
NullFacts = TypeofEQObject | TypeofNEString | TypeofNENumber | TypeofNEBigInt | TypeofNEBoolean | TypeofNESymbol | TypeofNEFunction | TypeofNEHostObject | EQNull | EQUndefinedOrNull | NEUndefined | Falsy | IsNull,
1275-
EmptyObjectStrictFacts = All & ~(EQUndefined | EQNull | EQUndefinedOrNull | IsUndefinedOrNull),
1276-
EmptyObjectFacts = All & ~IsUndefinedOrNull,
1277-
UnknownFacts = All & ~IsUndefinedOrNull,
1276+
UndefinedFacts = TypeofNEString | TypeofNENumber | TypeofNEBigInt | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | TypeofNEFunction | TypeofNEHostObject | EQUndefined | EQUndefinedOrNull | NENull | Falsy | IsUndefined | IsPossiblyUndefined,
1277+
NullFacts = TypeofEQObject | TypeofNEString | TypeofNENumber | TypeofNEBigInt | TypeofNEBoolean | TypeofNESymbol | TypeofNEFunction | TypeofNEHostObject | EQNull | EQUndefinedOrNull | NEUndefined | Falsy | IsNull | IsPossiblyNull,
1278+
EmptyObjectStrictFacts = All & ~(EQUndefined | EQNull | EQUndefinedOrNull | IsUndefinedOrNull | IsPossiblyUndefinedOrNull),
1279+
EmptyObjectFacts = All & ~(IsUndefinedOrNull | IsPossiblyUndefinedOrNull),
1280+
UnknownStrictFacts = All & ~IsUndefinedOrNull,
1281+
UnknownFacts = All & ~(IsUndefinedOrNull | IsPossiblyUndefinedOrNull),
12781282
AllTypeofNE = TypeofNEString | TypeofNENumber | TypeofNEBigInt | TypeofNEBoolean | TypeofNESymbol | TypeofNEObject | TypeofNEFunction | NEUndefined,
12791283
// Masks
12801284
OrFactsMask = TypeofEQFunction | TypeofNEObject,
@@ -27477,7 +27481,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2747727481
if (flags & TypeFlags.Intersection) {
2747827482
return getIntersectionTypeFacts(type as IntersectionType, callerOnlyNeeds);
2747927483
}
27480-
return TypeFacts.UnknownFacts;
27484+
return strictNullChecks && flags & TypeFlags.Unknown ? TypeFacts.UnknownStrictFacts : TypeFacts.UnknownFacts;
2748127485
}
2748227486

2748327487
function getIntersectionTypeFacts(type: IntersectionType, callerOnlyNeeds: TypeFacts): TypeFacts {
@@ -33699,7 +33703,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3369933703
}
3370033704
error(
3370133705
node,
33702-
facts & TypeFacts.IsUndefined ? facts & TypeFacts.IsNull ?
33706+
facts & TypeFacts.IsPossiblyUndefined ? facts & TypeFacts.IsPossiblyNull ?
3370333707
Diagnostics._0_is_possibly_null_or_undefined :
3370433708
Diagnostics._0_is_possibly_undefined :
3370533709
Diagnostics._0_is_possibly_null,
@@ -33709,7 +33713,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3370933713
else {
3371033714
error(
3371133715
node,
33712-
facts & TypeFacts.IsUndefined ? facts & TypeFacts.IsNull ?
33716+
facts & TypeFacts.IsPossiblyUndefined ? facts & TypeFacts.IsPossiblyNull ?
3371333717
Diagnostics.Object_is_possibly_null_or_undefined :
3371433718
Diagnostics.Object_is_possibly_undefined :
3371533719
Diagnostics.Object_is_possibly_null,
@@ -33720,7 +33724,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3372033724
function reportCannotInvokePossiblyNullOrUndefinedError(node: Node, facts: TypeFacts) {
3372133725
error(
3372233726
node,
33723-
facts & TypeFacts.IsUndefined ? facts & TypeFacts.IsNull ?
33727+
facts & TypeFacts.IsPossiblyUndefined ? facts & TypeFacts.IsPossiblyNull ?
3372433728
Diagnostics.Cannot_invoke_an_object_which_is_possibly_null_or_undefined :
3372533729
Diagnostics.Cannot_invoke_an_object_which_is_possibly_undefined :
3372633730
Diagnostics.Cannot_invoke_an_object_which_is_possibly_null,
@@ -33743,8 +33747,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3374333747
error(node, Diagnostics.Object_is_of_type_unknown);
3374433748
return errorType;
3374533749
}
33746-
const facts = getTypeFacts(type, TypeFacts.IsUndefinedOrNull);
33747-
if (facts & TypeFacts.IsUndefinedOrNull) {
33750+
const facts = getTypeFacts(type, TypeFacts.IsPossiblyUndefinedOrNull);
33751+
if (facts & TypeFacts.IsPossiblyUndefinedOrNull) {
3374833752
reportError(node, facts);
3374933753
const t = getNonNullableType(type);
3375033754
return t.flags & (TypeFlags.Nullable | TypeFlags.Never) ? errorType : t;

0 commit comments

Comments
 (0)