Open benlesh opened 3 years ago
This is apparently a limitation of TypeScript, and there's nothing we can really do here, AFAICT. cc @DanielRosenwasser: Just to be sure, there's not really a way to do this with TypeScript is there? Array reduce
seems to have similar issues.
console.log([1, 2, 3].reduce((acc, value) => {
if (acc === null) {
return '' + value;
} else {
return acc + ', ' + value;
}
}, null))
I searched here, but didn't see anything: https://github.com/microsoft/TypeScript/issues?q=is%3Aissue+is%3Aopen+%22reduce%22+label%3A%22Design+Limitation%22+
If filed a related issue at https://github.com/microsoft/TypeScript/issues/46438 for the TypeScript team to track it. I'm sure they must know about this, but I didn't see an issue where it was being tracked.
Frequently, a user may want to initialize state with some initial value so the scan is called on each pass, but the reducer will never return that value type.
In this scenario the types still come out wrong:
Or maybe it's this, which will have all sorts of type errors: