From c9fa6879ad2a8a419474fbda67771ee04c6dd8c2 Mon Sep 17 00:00:00 2001 From: Homa Wong Date: Sun, 20 Nov 2016 21:48:36 -0800 Subject: [PATCH] Remove optional designation. 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 --- src/index.d.ts | 4 ++-- test/typings-test.ts | 12 +++++++++++- tsconfig.json | 4 +++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index 2a1b2d0..375ac65 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -11,7 +11,7 @@ export interface FluxStandardAction { * 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. @@ -23,7 +23,7 @@ export interface FluxStandardAction { * 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 extends FluxStandardAction { diff --git a/test/typings-test.ts b/test/typings-test.ts index efdd53f..634bfee 100644 --- a/test/typings-test.ts +++ b/test/typings-test.ts @@ -12,7 +12,7 @@ interface MyError extends Error { someField: string; } -function createCustomAction(payload: CustomPayload): FluxStandardAction { +function createCustomAction(payload: CustomPayload) { return { type: 'custom', payload @@ -31,6 +31,16 @@ function isCustomAction3(action: any): action is FluxStandardAction { + 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(action)) { let a: number = action.payload.a; diff --git a/tsconfig.json b/tsconfig.json index b608449..3989eef 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,8 @@ { "compilerOptions": { - "outDir": "tsc-out" + "outDir": "tsc-out", + "strictNullChecks": true, + "target": "es5" }, "files": [ "test/typings-test.ts"