TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

5.6 regression: Array.prototype.reduce type inference regression

Open trevorade opened this issue 5 months ago • 7 comments

🔎 Search Terms

Inference, Reduce

🕗 Version & Regression Information

  • This changed between versions 5.5.4 and 5.6.0-beta (still happening in 5.6.1-rc)

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.6.1-rc#code/JYOwLgpgTgZghgYwgAgMoHsC2EAqBPABxQG8AoZC5CEAVwC40sIBRWzAblIF9TTqbMjbKwHIylZAEFkAXmQBGADTlKAIVnIATMokBhDQGYdlACIaALMp6kAJhAQAbOFBQJ0IAM5hkANzgOPBgxsfCIAbQBdTlI3T28PTH8HCC8RDT8AgDoXGxokAAoVCnzMUBFFX38ASlkAPmQAWTgwAAtM0pASstoKjMz+KuMKYJY2TJNlKs4EpJSwEU4AekXkAD0AfiA

💻 Code

interface SomeType {
    enu: SomeEnum;
}

enum SomeEnum {
    A = 1,
    B = 2,
    C = 3,
    D = 4,
}

declare const vals: SomeType[];

const smallestEnu = vals.reduce(
    (minEnu, val) => Math.min(minEnu, val.enu),
    SomeEnum.D,
);
smallestEnu;
// ^?

🙁 Actual behavior

smallestEnu has type number

🙂 Expected behavior

smallestEnu has type SomeEnum as it did in previous releases

Additional information about the issue

Interestingly, this does not repro if you remove the container type: https://www.typescriptlang.org/play/?ts=5.6.1-rc#code/KYOwrgtgBAyg9hYBRc0DeAoK2oEEoC8UAjADRY4BChUATOTlAMI0DMDOAIjQCzkC+GDABNgAYwA2AQwBOwKGLggAzgBcooSMoBcsBMlQBtALoBuIYpXrlEKRInA1KMDU0RlAOjnCwY4AAoKbH8IAEsQZ1INcABKQgA+KABZKVUACw8wkBDwyOiwGI5seERnCA9OchjzGzsHJ3BzAHomqAA9AH4MIA

(Google note: relevant source location http://shortn/_JgMRGaMa4e)

trevorade avatar Aug 26 '24 19:08 trevorade