Don't issue implicit any when obtaining the implied type for a binding pattern (#60083)

Co-authored-by: Jake Bailey <5341706+jakebailey@users.noreply.github.com>
This commit is contained in:
Anders Hejlsberg
2024-09-30 21:38:53 -07:00
committed by GitHub
co-authored by Jake Bailey
parent 35902c2e77
commit ca18009b8b
12 changed files with 863 additions and 2 deletions
+6 -2
View File
@@ -11633,7 +11633,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// contextual type or, if the element itself is a binding pattern, with the type implied by that binding
// pattern.
const contextualType = isBindingPattern(element.name) ? getTypeFromBindingPattern(element.name, /*includePatternInType*/ true, /*reportErrors*/ false) : unknownType;
return addOptionality(widenTypeInferredFromInitializer(element, checkDeclarationInitializer(element, CheckMode.Normal, contextualType)));
return addOptionality(getWidenedLiteralTypeForInitializer(element, checkDeclarationInitializer(element, CheckMode.Normal, contextualType)));
}
if (isBindingPattern(element.name)) {
return getTypeFromBindingPattern(element.name, includePatternInType, reportErrors);
@@ -40523,7 +40523,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
function widenTypeInferredFromInitializer(declaration: HasExpressionInitializer, type: Type) {
const widened = getCombinedNodeFlagsCached(declaration) & NodeFlags.Constant || isDeclarationReadonly(declaration) ? type : getWidenedLiteralType(type);
const widened = getWidenedLiteralTypeForInitializer(declaration, type);
if (isInJSFile(declaration)) {
if (isEmptyLiteralType(widened)) {
reportImplicitAny(declaration, anyType);
@@ -40537,6 +40537,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return widened;
}
function getWidenedLiteralTypeForInitializer(declaration: HasExpressionInitializer, type: Type) {
return getCombinedNodeFlagsCached(declaration) & NodeFlags.Constant || isDeclarationReadonly(declaration) ? type : getWidenedLiteralType(type);
}
function isLiteralOfContextualType(candidateType: Type, contextualType: Type | undefined): boolean {
if (contextualType) {
if (contextualType.flags & TypeFlags.UnionOrIntersection) {