-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Types for functions like Array.prototype.reduce or RxJS's "scan" seem impossible #46438
Comments
I think this is covered by #36554. I usually handle this with something similar to @nmain’s example, by adding an annotation on the accumulator. https://www.typescriptlang.org/play?#code/PTAEBUAsFNQFwJ4AdYHsBmoBO0DOBXAGzlAEtdQADAO3wFsAjaLSgGlAfxN0lSIBMOsSrjhZS1AOaUAdAFgAUIoDGqaqOx4iJALygA2gEZ2AJnYBmALoyc-fMugAKRwENlygFyhahQqAA+oKLiUuwAbi6E+NAAlKA6AHygAN6KoOlkmK7u8Tp6PoRxqQoZpZpw+FjUoADkNaAA1KARUdAA3GkZAL6g0IS4sMVlGTgVVaBuyo217PVNLdEdJd2KXewFMUsqariohNAyhKiSjjgExJugIKAARMagZqDmN9vqewdHJ4goGJrncJdrjdghJJDcgA |
Our inference process is so fundamentally different here, but it does feel bad that when we have plain local statements, we're able to dive into the body of this loop and know it's either been set to function foo(xs: number[]) {
let acc = null;
for (const value of xs) {
if (acc === null) {
acc = "" + value;
}
else {
acc = acc + ", " + value;
}
}
return acc;
} I think at some point, I would like to pick @ahejlsberg's brain to understand how control flow analysis deals with these sorts of circularities (it's been a while for me, so I've largely forgotten). Maybe we could brainstorm on something that would work better with higher order functions like these. One issue with our inference process is that in our first pass going through arguments, we "lock in" inferences from expressions that aren't "contextually sensitive" (e.g. callbacks). The
|
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Yeah. I'm familiar with the approach of @nmain. However, I would strongly advise people that they should leverage inference and not pass explicit generics. I say this because generics are just another element of an API signature that can change over major versions. So |
Honestly, @DanielRosenwasser, I would rather appreciate this specific issue remain open, because I feel that this issue is unique to a reduce-type callback situation. As we see it in RxJS's |
@benlesh are you asking for automatic union type promotion ? Because in the example shown - there is nothing impossible here. Type error can be trivially fixed by the user:
ps: i understand that many people would not agree on abandoning the use of null for default values, so those ones can type
How can (and why should) Typescript promote |
Bug Report
I searched, and couldn't find a related issue (but I find it very unlikely one doesn't exist). It seems like there should be something labeled "Design Limitation" or the like?
The basic problem is if you're using a standard "reducer" callback pattern with a seed that is different from what your reducer returns the returned type is incorrect.
🔎 Search Terms
is:issue is:open array reduce
is:issue is:open reduce callback
is:issue is:open reduce "No overload matches this call"
is:issue is:open reduce label:"Design Limitation"
🕗 Version & Regression Information
Any version of TS, to TMK. Specifically tested in 4.2 and 4.4.4
⏯ Playground Link
An example in the playground
💻 Code
🙁 Actual behavior
A type error, even though the compiled JavaScript is valid.
🙂 Expected behavior
No type error,
result
should be a typestring
, notnumber
.Related:
ReactiveX/rxjs#6649
The text was updated successfully, but these errors were encountered: