Skip to content

Commit 4475012

Browse files
committed
Improve getNonNullableType function
1 parent 6df16b3 commit 4475012

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

Diff for: src/compiler/checker.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -20320,14 +20320,17 @@ namespace ts {
2032020320
}
2032120321

2032220322
function getGlobalNonNullableTypeInstantiation(type: Type) {
20323+
// First reduce away any constituents that are assignable to 'undefined' or 'null'. This not only eliminates
20324+
// 'undefined' and 'null', but also higher-order types such as a type parameter 'U extends undefined | null'
20325+
// that isn't eliminated by a NonNullable<T> instantiation.
20326+
const reducedType = getTypeWithFacts(type, TypeFacts.NEUndefinedOrNull);
2032320327
if (!deferredGlobalNonNullableTypeAlias) {
2032420328
deferredGlobalNonNullableTypeAlias = getGlobalSymbol("NonNullable" as __String, SymbolFlags.TypeAlias, /*diagnostic*/ undefined) || unknownSymbol;
2032520329
}
20326-
// Use NonNullable global type alias if available to improve quick info/declaration emit
20327-
if (deferredGlobalNonNullableTypeAlias !== unknownSymbol) {
20328-
return getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [type]);
20329-
}
20330-
return getTypeWithFacts(type, TypeFacts.NEUndefinedOrNull); // Type alias unavailable, fall back to non-higher-order behavior
20330+
// If the NonNullable<T> type is available, return an instantiation. Otherwise just return the reduced type.
20331+
return deferredGlobalNonNullableTypeAlias !== unknownSymbol ?
20332+
getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [reducedType]) :
20333+
reducedType;
2033120334
}
2033220335

2033320336
function getNonNullableType(type: Type): Type {

0 commit comments

Comments
 (0)