Fix exported types so that they work for react-redux
See original GitHub issueIt seems the typing for react redux hook useSelector
relies on the types of the equality function to infer the return type:
// @types/react-redux/index.d.ts
export function useSelector<TState, TSelected>(
selector: (state: TState) => TSelected,
equalityFn?: (left: TSelected, right: TSelected) => boolean
): TSelected;
Using isEqual
as equality function the compiler will infer any
as TSelected
type because:
declare module 'react-fast-compare' {
const isEqual: (a: any, b: any) => boolean
export default isEqual
}
So
const selector = (state: State): DeepObject => {a: { b: [1,2,3] } }
const value = useSelector(selector) // typed correctly as DeepObject
const value = useSelector(selector, isEqual) // typed as any
Declaring isEqual
with generic types should solve the issue
declare module 'react-fast-compare' {
const isEqual: <A, B>(a: A, b: B) => boolean
export default isEqual
}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:8 (6 by maintainers)
Top Results From Across the Web
Usage with TypeScript - React Redux
Since those are types, it's safe to export them directly from your store setup file such as app/store.ts and import them directly into...
Read more >useSelector is not able to export from 'react-redux'
React Redux hooks were first added in v7.1.0 so you can't import useSelector hook if you are using React Redux v6.14.6 as you...
Read more >How to build a type-safe React Redux app - LogRocket Blog
First, we'll create the essential Redux building blocks — namely, action creator, reducer, and store. If you're not familiar with these building ...
Read more >How to Use Redux Toolkit with TypeScript - Fullstack.io
First of all, we need to create a new app. We can use Create React App with TypeScript template and then add Redux...
Read more >A type-safe approach to Redux stores in TypeScript - Medium
export type ChatActions = UsersListUpdatedAction | MessageReceivedAction; ... react-router has its own Redux middleware, so we'll use this
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@chrisbolin We may be able to retain support as it was before but satisfy typings like these by using:
<A = any, B = any>(a: A, b: B) => boolean
@ryan-roemer this might be closable as well. We may not have had the automation turned on to set this ticket to closed when moving the card on the project board.