#Typing reduce params
30 messages · Page 1 of 1 (latest)
you can specify the type parameter of reduce
I've used type assertion on the initialValue of my reduce, is that the way you'd approach or do you mean type something else?
reduce<number>(...), just specifying the type parameter of reduce?
sortedList.reduce((acc, movement, index) => {}, [] as (ProductFrom | ProductTo)[]);
is what i have
works all fine but i wonder if i should be expliclity typing differently or typing something else
...the type parameter of reduce
that code doesn't work though, (ProductFrom, ProductTo) isn't valid
did you mean [ProductFrom, ProductTo]
they mean completely different things
Ah i wasn't aware
it absolutely matters 💀
What's the difference?
i don't know what you actually want there though so i can't say if you want a tuple or a union
uh, everything? you'd get a better answer from "what're the similarities" and that answer would be "they're typescript types" and nothing else, really
I want to say the return type is an array, where the types in the array can be ProductFrom or ProductTo
that would be a union then
anyways, this is more about code style consistency then
in your codebase, if you want a variable of type string[] let's say, would you do const arr: string[] = []; or const arr = [] as string[];
if the latter, then your current code would be following that pattern, but if the former you'd use the type parameter
The former, ok so that's basically what im getting at
they mean the same thing
That's really useful ty
Is there somewhere i can read up about TS tuples btw? all i can see from offical ts is a playground link about them
tyvm