tsc: 2.2.0-dev.20161116
Discover this behavior from redux-utilities/flux-standard-action@78a9065
This is the original code:
exportinterfaceFSA<Payload,Meta>{
...
payload?: Payload;
...
}However, this does not work well with type guard:
functionisSomeAction(action: any): action is FSA<{message: string},void>{returntrue;}letaction={...};if(isSomeAction(action)){// `action.payload` may be undefined.console.log(action.payload.message);}Since generic type can be anything, including undefined, I'm considering to remove the optional designation:
exportinterfaceFSA<Payload,Meta>{
...
payload: Payload;
...
}// now type guard worksletaction={...};if(isSomeAction(action)){console.log(action.payload.message);}However, now the creation code fail:
functioncreateSomeAction(): FSA<undefined,undefined>{// error: `payload` and `meta` are requiredreturn{type: 'SOME_ACTION'};}The workaround is to depends on infer type:
functioncreateSomeAction(){return{type: 'SOME_ACTION'};}or specify it:
functioncreateSomeAction(): FSA<undefined,undefined>{return{type: 'SOME_ACTION',payload: undefined,meta: undefined};}Is there a better solution? 🌷
tsc: 2.2.0-dev.20161116
Discover this behavior from redux-utilities/flux-standard-action@78a9065
This is the original code:
However, this does not work well with type guard:
Since generic type can be anything, including
undefined, I'm considering to remove the optional designation:However, now the creation code fail:
The workaround is to depends on infer type:
or specify it:
Is there a better solution? 🌷