Skip to content

Issue implicit any in JS files on widened inferred types from initializers in binding patterns #1064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

Andarist
Copy link
Contributor

@Andarist Andarist commented Jun 5, 2025

@Copilot Copilot AI review requested due to automatic review settings June 5, 2025 12:30
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR ports missing logic from #60083 to improve how JS initializers produce widened types and report implicit any in empty literals.

  • Adds getWidenedLiteralTypeForInitializer to centralize literal widening.
  • Extends widenTypeInferredFromInitializer to report and return any/any[] for empty literals in JS files.
  • Updates binding-element handling to use the new helper.

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.

File Description
internal/checker/checker.go Introduces getWidenedLiteralTypeForInitializer and adjusts widenTypeInferredFromInitializer; updates binding-element call site.
testdata/baselines/reference/.../*.types Updates expected baselines to reflect any/any[] instead of never for empty initializers.
testdata/baselines/reference/.../*.errors.txt Adjusts error baselines to include implicit-any diagnostics for empty literals.
Comments suppressed due to low confidence (1)

internal/checker/checker.go:17022

  • This change bypasses widenTypeInferredFromInitializer, which includes JS-specific implicit-any diagnostics for empty initializers. Consider calling widenTypeInferredFromInitializer here instead of getWidenedLiteralTypeForInitializer to preserve those diagnostics.
return c.addOptionality(c.getWidenedLiteralTypeForInitializer(element, c.checkDeclarationInitializer(element, CheckModeNormal, contextualType)))

@jakebailey jakebailey requested a review from sandersn June 5, 2025 15:47
@jakebailey
Copy link
Member

Can you remerge main here?

@Andarist
Copy link
Contributor Author

done

json = []
+ ~~~~
+!!! error TS7031: Binding element 'json' implicitly has an 'any[]' type.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also tried porting this and hit these same issues. I'm pretty sure these are exactly the things that we are aiming to prevent, but now I think the fix doesn't quite work anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a look at this tomorrow~ then

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR aims to issue implicit any error - and it does that... to some extent 😉 It is overly eager though. From what I can see, this one is still not handled by the reparser - and that's why we are seeing errors here. The compiler thinks it doesn't have a type annotation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @a-tarasyuk had a recent PR that improves handling of destructured parameters. Do things improve if you merge from main?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes! destructuringParameterDeclaration9(strict=true).errors.txt.diff is completely gone now

@Andarist Andarist marked this pull request as draft June 23, 2025 18:59
…y-widening-binding-patterns

# Conflicts:
#	testdata/baselines/reference/submodule/conformance/destructuringParameterDeclaration9(strict=true).types
#	testdata/baselines/reference/submoduleAccepted/conformance/destructuringParameterDeclaration9(strict=true).types.diff
@Andarist Andarist marked this pull request as ready for review July 1, 2025 07:33
@Andarist Andarist requested a review from jakebailey July 1, 2025 07:33
@@ -17212,7 +17227,7 @@ func (c *Checker) getTypeFromBindingElement(element *ast.Node, includePatternInT
if ast.IsBindingPattern(element.Name()) {
contextualType = c.getTypeFromBindingPattern(element.Name(), true /*includePatternInType*/, false /*reportErrors*/)
}
return c.addOptionality(c.widenTypeInferredFromInitializer(element, c.checkDeclarationInitializer(element, CheckModeNormal, contextualType)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this new function is called from a couple of other places in Strada. Why not in Corsa too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only see 3 getWidenedLiteralTypeForInitializer hits in both codebases 🤔 And they seem to be in the same places. Same for widenTypeInferredFromInitializer (4 hits)

Copy link
Member

@jakebailey jakebailey Jul 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is that possible? This PR is introducing getWidenedLiteralTypeForInitializer and only adds one call

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it splits the old widenTypeInferredFromInitializer into widenTypeInferredFromInitializer and getWidenedLiteralTypeForInitializer. You have the second call to getWidenedLiteralTypeForInitializer at the top of the new version of widenTypeInferredFromInitializer

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but the old code had other calls to the get variant

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I'm wrong, it seems fine

Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diffs now look good but the new function is called two other places in Strada, so maybe there are more diffs that could be eliminated?

@jakebailey jakebailey requested a review from sandersn July 1, 2025 16:11
@jakebailey jakebailey added this pull request to the merge queue Jul 1, 2025
Merged via the queue into microsoft:main with commit 2877b6f Jul 1, 2025
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants