Skip to content

Binding element incorrectly reported as having any type in JS with JSDoc starting with 5.6.2 #59936

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

Closed
burtek opened this issue Sep 10, 2024 · 4 comments Β· Fixed by #60083
Closed
Labels
Bug A bug in TypeScript
Milestone

Comments

@burtek
Copy link

burtek commented Sep 10, 2024

πŸ”Ž Search Terms

Binding element implicitly has an any type 7031

πŸ•— Version & Regression Information

  • This changed between versions 5.5.4 and 5.6.2

⏯ Playground Link

https://www.typescriptlang.org/play/?strictNullChecks=true&target=9&suppressImplicitAnyIndexErrors=true&ts=5.6.2&filetype=js#code/PQKhCgAIUgBAHAhgJ0QW0gbwPICMBWApgMYAuAvpANrED2AdgGYCWA5gLpQwIrpYAKKUs0QAbADwAlErWQATcQHJ8AZwaLIAH0jK19Yhu26GAVkUAaSCtLJm9VlXYA+J5RoMWrAHSI5c5sIMYgBizKKEKpzQwOCEAB7wsqSQjACu+oH0kPDIhEi5AMIebAAUmFCQlb7+mSFhEQBcWBWVrZCqDJAAvNRRrZQ9mOTgA1jkAJTNbXT0auFeorSsJR304yNAA

πŸ’» Code

/**
 * @param {Object} [config]
 * @param {Partial<Record<'json' | 'jsonc' | 'json5', string[]>>} [config.additionalFiles]
 */
export function prepareConfig({
    additionalFiles: {
        json = []
    } = {}
} = {}) {
    console.log(json)
}

πŸ™ Actual behavior

json is errored as Binding element 'json' implicitly has an 'any[]' type.(7031) even though it's typed as string[]

image

πŸ™‚ Expected behavior

No error expected, as json is typed as string[]

Additional information about the issue

This worked well in 5.5.4 but broke with upgrade to 5.6.2. It works correctly in .ts file, but breaks in .js file with JSDoc.

@Andarist
Copy link
Contributor

I'll fix this as part of #59924

@burtek
Copy link
Author

burtek commented Sep 10, 2024

@Andarist ok, thanks. I guess it's gonna go out as 5.6 patch bump?

@Andarist
Copy link
Contributor

Andarist commented Sep 15, 2024

@burtek in the end I decided to create a separate PR for this one since I was able to fix this independently and the other PR raises some design questions. The fix is available here: #59975

As to your question, this is a recent regression so I'd advocate to bacport a fix to 5.6 but that's up to the TS team.

@ahejlsberg
Copy link
Member

ahejlsberg commented Sep 27, 2024

This issue actually existed before 5.6, but only for array binding patterns (playground link):

/**
 * @param {[[string[]?]?]} [x]
 */
function f1([[json = []] = []] = []) { return json } // Implicit any[] error on json binding element

In 5.6 we aligned array and object literal padding and the issue then showed up in object binding patterns as well:

/**
 * @param {{ a?: { json?: string[] }}} [x]
 */
function f2({ a: { json = [] } = {} } = {}) { return json } // Implicit any[] error on json binding element

The culprit here is logic we have in widenTypeInferredFromInitializer to issue no-implicit-any errors in JS files. We shouldn't be issuing these errors when we're obtaining the implied type for a binding pattern. I'll put up a PR to fix. Unfortunately the fix in #59975 isn't the right one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
4 participants