fix(43030): fix instantiated null/undefined type from JS initializer (#43933)

This commit is contained in:
Oleksandr T
2021-05-12 01:57:16 +03:00
committed by GitHub
parent bb6d8a716e
commit 463c79440f
15 changed files with 366 additions and 22 deletions

View File

@@ -20025,9 +20025,13 @@ namespace ts {
return (type as TypeReference).cachedEquivalentBaseType = instantiatedBase;
}
function isEmptyLiteralType(type: Type): boolean {
return strictNullChecks ? type === implicitNeverType : type === undefinedWideningType;
}
function isEmptyArrayLiteralType(type: Type): boolean {
const elementType = getElementTypeOfArrayType(type);
return strictNullChecks ? elementType === implicitNeverType : elementType === undefinedWideningType;
return !!elementType && isEmptyLiteralType(elementType);
}
function isTupleLikeType(type: Type): boolean {
@@ -32355,7 +32359,7 @@ namespace ts {
function widenTypeInferredFromInitializer(declaration: HasExpressionInitializer, type: Type) {
const widened = getCombinedNodeFlags(declaration) & NodeFlags.Const || isDeclarationReadonly(declaration) ? type : getWidenedLiteralType(type);
if (isInJSFile(declaration)) {
if (widened.flags & TypeFlags.Nullable) {
if (isEmptyLiteralType(widened)) {
reportImplicitAny(declaration, anyType);
return anyType;
}