TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Using variable defined by array destructuring assignment in default value of variables defined afterwards.

Open farazsa88 opened this issue 3 years ago • 1 comments

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.

farazsa88 avatar Jul 21 '22 18:07 farazsa88

@RyanCavanaugh I've submitted a PR for this issue. Would you please check it later?

babakks avatar Aug 09 '22 21:08 babakks

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)

Playground link

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 avatar Dec 12 '23 19:12 jcalz

@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.

babakks avatar Jan 11 '24 12:01 babakks

Indeed, looks like it. And since that got merged it's also visible in the nightly build.

jcalz avatar Jan 11 '24 13:01 jcalz