본문 바로가기

JavaScript/TypeScript

useSelector에서 RootState 사용 안 하는 법

보통 useSelector를 사용할 때 아래와 같이 store의 타입을 선언해줘야 했다.

  const { data } = useSelector((state: RootState) => state.home);

 

아래와 같이 index.d.ts에 모듈 선언을 해주면 RootState 타입 선언 없이 사용 할 수 있다.

import 'react-redux';
import { RootState } from '@src/store/store';

declare module 'react-redux' {
  interface DefaultRootState extends RootState { };
}
  const { data } = useSelector(state => state.home);

 

https://stackoverflow.com/questions/60777859/ts2339-property-tsreducer-does-not-exist-on-type-defaultrootstate