-
Notifications
You must be signed in to change notification settings - Fork 13k
Open
Labels
CommittedThe team has roadmapped this issueThe team has roadmapped this issueHelp WantedYou can do thisYou can do thisSuggestionAn idea for TypeScriptAn idea for TypeScript
Milestone
Description
tsc: 2.2.0-dev.20161116
Discover this behavior from redux-utilities/flux-standard-action@78a9065
This is the original code:
export interface FSA<Payload, Meta> {
...
payload?: Payload;
...
}
However, this does not work well with type guard:
function isSomeAction(action: any): action is FSA<{ message: string }, void> {
return true;
}
let action = {...};
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:
export interface FSA<Payload, Meta> {
...
payload: Payload;
...
}
// now type guard works
let action = {...};
if (isSomeAction(action)) {
console.log(action.payload.message);
}
However, now the creation code fail:
function createSomeAction(): FSA<undefined, undefined> {
// error: `payload` and `meta` are required
return { type: 'SOME_ACTION' };
}
The workaround is to depends on infer type:
function createSomeAction() {
return { type: 'SOME_ACTION' };
}
or specify it:
function createSomeAction(): FSA<undefined, undefined> {
return {
type: 'SOME_ACTION',
payload: undefined,
meta: undefined
};
}
Is there a better solution? 🌷
ssynix, listepo-alterpost, AlexeyMz, demurgos, topaxi and 62 moreagalazis, FanAs and Avrmasteragalazis, brainkim, rope-hmg, FanAs and Avrmaster
Metadata
Metadata
Assignees
Labels
CommittedThe team has roadmapped this issueThe team has roadmapped this issueHelp WantedYou can do thisYou can do thisSuggestionAn idea for TypeScriptAn idea for TypeScript