타입스크립트 리덕스 예시

Column 1
Tags
example
TS
Column
 

initialState 타입 지정하기

1) 타입 선언하기

type CounterState { value: number } // Define the initial state using that type const initialState: CounterState = { value: 0 }

2) 타입 캐스팅하기

경우에 따라 타입스크립트가 상태의 형태를 너무 타이트하게 강제한다고 느껴질 때가 있다.
그렇다면 타입을 선언하는 대신 타입 캐스팅을 활용하는 것도 방법이 될 수 있다. (근데 비추)
// Workaround: cast state instead of declaring variable type const initialState = { value: 0 } as CounterState