reduxjs / reselect

Selector library for Redux
MIT License
19.03k stars 671 forks source link

Incorrect return type from createSelector when selector returns union of types with keys in common and undefined #723

Closed oloftus closed 3 months ago

oloftus commented 3 months ago

First off, thanks for the reduxjs project!

Here is a description of what I think is a bug.

See this repro for a reproduction. Reproduced with reselect@5.1.1.

NOK

When a selector returns a type A | B | undefined, and types A and B have a key in common, the return type of the selector is A | undefined instead of A | B or undefined.

OK

When types A and B do not have a key in common, the return type is correct, i.e. A | B | undefined.

OK

When types A and B have a key in common, but the return union type does not include undefined, the return type is correct, i.e. A | B.

EskiMojo14 commented 3 months ago

image this is normal TS behaviour - B is a subtype of A (any B is assignable to A), so TS simplifies to the widest type.

oloftus commented 3 months ago

Thanks for the swift response. You're quite right. I hadn't even considered it might be coming from TS and that it is normal behaviour.