Don't widen an all-optional selector result to any - #776
Conversation
✅ Deploy Preview for reselect-docs canceled.
|
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
|
if you have strictNullChecks off, are you even using Typescript? 😄 |
|
fair 😄 - it's a huge codebase still in the middle of that migration, so the flag isn't off by choice. either way unknown extends T isn't a test for unknown - under strict that's just indistinguishable, so behaviour there is unchanged. and it fails silently: you don't get an error, you get any. |
|
Yeah, our general policy for all Redux-related libs is we require use of This PR by itself seems passable at first glance, I'm just not sure I want to actually add a change that relates to strict checks being off. |
|
Please Please consider adding this change, I can relate very much to @veksa 's request. Huge code base, we're in the middle of migration🙏🙏🙏 |
|
Yeah, that's fair. And I definitely appreciate hearing that other people are experiencing this issue and would benefit from the fix (and also that you're migrating to fix things :) ) I think we have some other fixes that have been merged and not released. I'll land this, and I'll look at trying to get out a new version later today. |
With
strictNullChecks: false, a selector whose result has only optional properties loses its type:Nothing warns about it — the combiner argument just arrives as
anyand everything built on top quietly degrades. Most state slices have optional fields, so this hits a lot of selectors.The cause is
IfUnknown:unknown extends Tisn't a test forunknown. WithoutstrictNullChecks,undefinedinhabits every type, so a type with no required properties demands nothing andunknownis assignable to it — it gets mistaken forunknownand replaced by theanyfallback. Checkingkeyofinstead separates the two and reads the same under both settings.One caveat:
{}andunknownstay mutually assignable withoutstrictNullChecksand neither has keys, so a result of exactly{}is still widened. That's unchanged from today, and documented on the type.Tests cover the helpers and
createSelectorend to end. They run twice — understrictvia the existing typecheck, and understrictNullChecks: falsevia a new tsconfig, since the bug doesn't reproduce understrict. It's wired intotype-check, sotestalready picks it up.