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

Incorrect type deducted for accumulator in reduce #59999

Closed
nielstron opened this issue Sep 18, 2024 · 2 comments
Closed

Incorrect type deducted for accumulator in reduce #59999

nielstron opened this issue Sep 18, 2024 · 2 comments

Comments

@nielstron
Copy link

πŸ”Ž Search Terms

"type inference reduce" "type inference tuple type"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=3.8.3&q=84#code/DYUwLgBMDOYFwQHYFcC2AjEAnA2gXXwgF4IccAGPPAbgChRIAPYicugYwHtFYIvOsAE2gIcKDNgA0SNJiwE8LGGAB0WEIOTsQACh0BDdu2lZOAd2kBLAJTEAfBADeEFy9quPUcBABWLNu6eLobsKgAOyNAAFjo4ltI+eNbUnoFB6mDIWIgQIXQeAL7S+NZAA

πŸ’» Code

let lst: number[][] = [[0]];
let x = 0;
const coords: [number, number][] = lst.reduce((acc, row, i) => {     
        let j = 0;
        acc.push([i, j]);     
        return acc;
    }, [])

πŸ™ Actual behavior

Typescript infers type number[]for acc and throws this error:

Type 'number[]' is not assignable to type '[number, number][]'.

πŸ™‚ Expected behavior

Typescript should infer type [number, number][].
When passing this type explicitely, the code compiles, i.e. it is possible to derive a correct type annotation.

let lst: number[][] = [[0]];
let x = 0;
const coords: [number, number][] = lst.reduce((acc: [number, number][], row, i) => {     
        let j = 0;
        acc.push([i, j]);     
        return acc;
    }, [])

https://www.typescriptlang.org/play/?ts=5.6.2&ssl=7&ssc=11&pln=1&pc=1&q=84#code/DYUwLgBMDOYFwQHYFcC2AjEAnA2gXXwgF4IccAGPPAbgChRIAPYicugYwHtFYIvOsAE2gIcKDNgA0SNJiwE8LGGAB0WEIOTsQACh0BDdu1Hi5009gXSsnAO7SAlgEpiAPggBvCN++0f-qHAIACsWNj8A70N2FQAHZGgACx0cB2lgvCdqAIjI9TBkLEQIaLp-AF9pfCcgA

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

MartinJohns commented Sep 18, 2024

This is working as intended. Duplicate of #59863.

@nielstron
Copy link
Author

I see, this seems right. Thank you!

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