Skip to content
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

The compiler ignores assignments inside array methods' callbacks #51869

Closed
DaviDevMod opened this issue Dec 12, 2022 · 2 comments
Closed

The compiler ignores assignments inside array methods' callbacks #51869

DaviDevMod opened this issue Dec 12, 2022 · 2 comments

Comments

@DaviDevMod
Copy link

Bug Report

Assignments inside array methods' callbacks are not taken into account for the purpose of typing variables.

There are many issues on array methods, eg: #16069, but I couldn't find a duplicate (original?).

🔎 Search Terms

array method callback

🕗 Version & Regression Information

The bug can be found in any version of the TypeScript Playground (currently from v3.3.3333 to 4.9.4, including the Nightly v5.0.0-dev.20221212).

⏯ Playground Link

Playground link with relevant code

💻 Code

const findFive = (array: number[]) => array.find((n) => n === 5);

const arrays = [[1], [2, 3], [4, 5, 6]];

let maybeAFive: number | undefined = undefined;

// `maybeAFive` is being assigned a `number | undefined` within an array method's callback.
arrays.find((array) => (maybeAFive = findFive(array)));

// It is still typed as `undefined` despite the assingment above.
maybeAFive;

// Imperative equivalent of the `.find()` call above.
for (const array of arrays) {
  maybeAFive = findFive(array);
  if (maybeAFive) break;
}

// Typed `number | undefined`, as expected.
maybeAFive;

🙁 Actual behavior

Assignments inside array methods' callbacks are not taken into account for the purpose of typing variables.

🙂 Expected behavior

Assignments inside array methods' callbacks should be taken into account for the purpose of typing variables.

@IllusionMH
Copy link
Contributor

Duplicate of #9998
Repo even contains specific temple for this kind of issues.

@DaviDevMod
Copy link
Author

@IllusionMH You are right, there is a "Types Not Correct in/with Callback" template.
That issue should mention "callback" somewhere in its title.
Anyway thanks for the answer.

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

No branches or pull requests

2 participants