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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Andarist
Copy link
Contributor

@Andarist Andarist commented Jun 5, 2025

This is still imperfect because JSDoc types are still WIP. This just ports some of the missing logic and some of the adjustments made in microsoft/TypeScript#60083 .

This PR is an oddball so feel free to close this as it might be better to revisit this when JSDoc types are more complete. It already fixes a couple of baselines though.

@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

@Andarist Andarist marked this pull request as draft June 23, 2025 18:59
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.

2 participants