Constants

부가설명
- type-constants 쓰지말고 - action creator앞에 static 붙여서 getType, isActionOf helper 사용하자
Tags
V4

Constants

RECOMMENDATION: typesafe-actions를 쓰면 string constants 들을 export하고 재사용할 필요가 없다. 이 라이브러리로 만들어진 action-creators는 action type을 static property로 가지고 있으며, action-helpers를 이용하여 쉽게 접근할 수 있고, 이를 reducers, epics, sagas 등에서 사용하면 된다. 코드도 간결하게 해준다.

타입스크립트에서 string constant 선언 시 주의점

Limitations of TypeScript when working with string constants - when using string constants as action type property, please make sure to use simple string literal assignment with const. This limitation is coming from the type-system, because all the dynamic string operations (e.g. string concatenation, template strings and also object used as a map) will widen the literal type to its super-type, string. As a result this will break contextual typing for action object in reducer cases.
// Example file: './constants.ts' // WARNING: Incorrect usage export const ADD = prefix + 'ADD'; // => string export const ADD = `${prefix}/ADD`; // => string export default { ADD: '@prefix/ADD', // => string } // Correct usage export const ADD = '@prefix/ADD'; // => '@prefix/ADD' export const TOGGLE = '@prefix/TOGGLE'; // => '@prefix/TOGGLE' export default ({ ADD: '@prefix/ADD', // => '@prefix/ADD' } as const) // working in TS v3.4 and above => https://github.com/Microsoft/TypeScript/pull/29510