🤖 Pick PR #44219 (Properly remove generic types that ...) into release-4.3 (#44243)

* Cherry-pick PR #44219 into release-4.3

Component commits:
4475012558 Improve getNonNullableType function

86f09ae87c Add tests

d45806a984 More closely match previous behavior

634b01ab3a Add non-strict mode test

* Update LKG

Co-authored-by: Anders Hejlsberg <andersh@microsoft.com>
Co-authored-by: typescript-bot <typescript@microsoft.com>
This commit is contained in:
TypeScript Bot
2021-05-25 11:40:42 -07:00
committed by GitHub
co-authored by Anders Hejlsberg typescript-bot
parent d6e6fa728e
commit 28e3e6ff2f
15 changed files with 460 additions and 77 deletions
+9 -6
View File
@@ -20239,14 +20239,17 @@ namespace ts {
}
function getGlobalNonNullableTypeInstantiation(type: Type) {
// First reduce away any constituents that are assignable to 'undefined' or 'null'. This not only eliminates
// 'undefined' and 'null', but also higher-order types such as a type parameter 'U extends undefined | null'
// that isn't eliminated by a NonNullable<T> instantiation.
const reducedType = getTypeWithFacts(type, TypeFacts.NEUndefinedOrNull);
if (!deferredGlobalNonNullableTypeAlias) {
deferredGlobalNonNullableTypeAlias = getGlobalSymbol("NonNullable" as __String, SymbolFlags.TypeAlias, /*diagnostic*/ undefined) || unknownSymbol;
}
// Use NonNullable global type alias if available to improve quick info/declaration emit
if (deferredGlobalNonNullableTypeAlias !== unknownSymbol) {
return getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [type]);
}
return getTypeWithFacts(type, TypeFacts.NEUndefinedOrNull); // Type alias unavailable, fall back to non-higher-order behavior
// If the NonNullable<T> type is available, return an instantiation. Otherwise just return the reduced type.
return deferredGlobalNonNullableTypeAlias !== unknownSymbol ?
getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [reducedType]) :
reducedType;
}
function getNonNullableType(type: Type): Type {
@@ -24035,7 +24038,7 @@ namespace ts {
}
function isGenericTypeWithUnionConstraint(type: Type) {
return !!(type.flags & TypeFlags.Instantiable && getBaseConstraintOrType(type).flags & TypeFlags.Union);
return !!(type.flags & TypeFlags.Instantiable && getBaseConstraintOrType(type).flags & (TypeFlags.Nullable | TypeFlags.Union));
}
function containsGenericType(type: Type): boolean {