fix(types/store): Unexpectedly narrowed return type of function Store['getState']
#4638
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
name: 🐛 Fix the return type of function
Store['getState']
about: Unexpectedly narrowed type
PR Type
Does this PR add a new feature, or fix a bug?
This PR fixes a bug in the type definitions.
Why should this PR be included?
What is the current behavior, and the steps to reproduce the issue?
The return type of function
Store['getState']
is unexpectedly narrowed to non-nullable.TS Playground: https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAbzjAnmApnASugJgVwGN0oAaOQqdAQxnQGUZpMBfOAMyghDgHIqCAD14AoEagxwAchAB2U-ABtF1AEaKGMWpgC8iChHyyYALjiz8IVSTgsRhOQGd4F5Wo04CxKGc9ESADwy8koq6pracAA+5qEAfHB6ABQAlIkJropiDrLOcM7MAOrAMAAWCm7hjJF6lDR0jMxJme7oft4p9k4uoa3VdIn5TFTFZRVhGv3oAHQA5ugwU6kiAPQrcBtwAHoA-GJAA
What is the expected behavior?
If
StateExt
type parameter is not provided,Store['getState']
should return the untouched typeS
.How does this PR fix the problem?
The problem is in this line:
redux/src/types/store.ts
Lines 114 to 119 in 7876f8e
If
StateExt
is not given a specific value, the type will beS & {}
which narrows the type to non-nullable.The behavior of intersection with
{}
is changed in TypeScript 4.8, "Improved Intersection Reduction, Union Compatibility, and Narrowing".In this PR:
StateExt
in theStore
interface is widened tounknown
. This can ensure thatS & unknown
is stillS
.StateExt
passed toStore
is widened using a utility typeUnknownIfNonSpecific
.Checklist