Using variable defined by array destructuring assignment in default value of variables defined afterwards.
Bug Report
🔎 Search Terms
Array destructuring assignment default value 7022
🕗 Version & Regression Information
Exists in 3.3.3 - 4.8.0-beta
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about this behaviour
⏯ Playground Link
Playground link with relevant code
💻 Code
const [a, b = a + 1] = [2];
console.log(`a: ${a}, b: ${b}`);
🙁 Actual behavior
Types of a and b inferred as any, or if noImplicitAny enabled, shows error: 'a' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.(7022)
🙂 Expected behavior
Types of both a and b should be inferred as number.
@RyanCavanaugh I've submitted a PR for this issue. Would you please check it later?
Not sure if the issue reported this SO question with the code:
declare const o: { a?: number, b?: number } | undefined
const { a, b = a } = o ?? {}; // error!
// ---> ~ 'a'
// implicitly has type 'any' because it does not have a type annotation and is
// referenced directly or indirectly in its own initializer.(7022)
is the same issue or not. Waiting to see if any fix for this addresses it; if not, I might open a new issue for it (unless someone can point to a more appropriate existing issue)
@jcalz I think the issue you mentioned is solved by the PR. You can check it via this custom-build playground. As I checked the type inference is now showing number | undefined for a and b symbols.
Indeed, looks like it. And since that got merged it's also visible in the nightly build.