2022/03/19 리액트ts에서 특정 reducer의 state를 지정시 컴파일 에러

2022. 3. 19. 00:24Project : 근의 공식(Muscle Formula)

임시방편 :any

testReducer가 분명 존재하고 분명 console 창에서는 testR이 제대로 띄워지고 있으나 React TS 상에서 컴파일 되지 않는 문제가 발생

이럴 때 임시 방편으로 state 인자에 any type을 선언해준다.

 

컴파일도 되고 콘솔에 testR도 제대로 띄워진다.

 

보다 근본적인 해결책

Redux 공식 문서 및 스택 오버플로우 참조 :

https://redux-toolkit.js.org/tutorials/typescript

 

TypeScript Quick Start | Redux Toolkit

 

redux-toolkit.js.org

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

 

TS2339: Property 'tsReducer' does not exist on type 'DefaultRootState'

Struggling with the above question. Seen similar questions but cannot figure it out. The below code is me attempting to open and close a dialog using TypeScript for first time in an existing React

stackoverflow.com

계속 개발하다가 Redux toolkit으로 바꾸었다. 그리고 reducer의 타입 지정을 위한 근본적인 해결책 또한 알게 되었다.

RootState, AppDispatch라는 이름으로 state와 dispatch의 타입 alias를 설정해준다.
이렇게 state와 dispatch와 타입을 지정하면 오류 없이 잘 컴파일된다.