question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fix exported types so that they work for react-redux

See original GitHub issue

It 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
kittencommented, Feb 6, 2020

@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

1reaction
cdierkenscommented, May 18, 2021

kale-stew moved this from In progress to Done in May 2020 on May 28, 2020

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found