Skip to content

Commit

Permalink
Remove optional designation.
Browse files Browse the repository at this point in the history
Since `payload` and `meta` is generic, user can specify `void` or `undefined` to indicate it does not present.

Removing the optional designation improves type guard.

see `isCustomAction4()` for example
  • Loading branch information
unional committed Dec 11, 2016
1 parent ee78552 commit c9fa687
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface FluxStandardAction<Payload, Meta> {
* By convention, if `error` is `true`, the `payload` SHOULD be an error object.
* This is akin to rejecting a promise with an error object.
*/
payload?: Payload;
payload: Payload;
/**
* The optional `error` property MAY be set to true if the action represents an error.
* An action whose `error` is true is analogous to a rejected Promise.
Expand All @@ -23,7 +23,7 @@ export interface FluxStandardAction<Payload, Meta> {
* The optional `meta` property MAY be any type of value.
* It is intended for any extra information that is not part of the payload.
*/
meta?: Meta
meta: Meta
}

export interface ErrorFluxStandardAction<CustomError extends Error, Meta> extends FluxStandardAction<CustomError, Meta> {
Expand Down
12 changes: 11 additions & 1 deletion test/typings-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface MyError extends Error {
someField: string;
}

function createCustomAction(payload: CustomPayload): FluxStandardAction<CustomPayload, any> {
function createCustomAction(payload: CustomPayload) {
return {
type: 'custom',
payload
Expand All @@ -31,6 +31,16 @@ function isCustomAction3(action: any): action is FluxStandardAction<void, string
return isFSA(action) && action.type === 'custom3';
}

function isCustomAction4(action: any): action is FluxStandardAction<{ message: string }, void> {
return true
}

let action2 = {}
if (isCustomAction4(action2)) {
// type guard infers payload will not be undefined
console.log(action2.payload.message)
}

function reducer(state, action) {
if (isFSA<CustomPayload, void>(action)) {
let a: number = action.payload.a;
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"compilerOptions": {
"outDir": "tsc-out"
"outDir": "tsc-out",
"strictNullChecks": true,
"target": "es5"
},
"files": [
"test/typings-test.ts"
Expand Down

0 comments on commit c9fa687

Please sign in to comment.