IssueRaiser

Treating `undefined` parameters as optional

tsc: 2.2.0-dev.20161116 Discover this behavior from https://github.com/acdlite/flux-standard-action/pull/45/commits/78a9065914b2ca4848dfba8fc0b47c54e2d0e319 This is the original code: ```ts export interface FSA<Payload, Meta> { ... payload?: Payload; ... } ``` However, this does not work well with type guard: ```ts 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: ```ts 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: ```ts function createSomeAction(): FSA<undefined, undefined> { // error: `payload` and `meta` are required return { type: 'SOME_ACTION' }; } ``` The workaround is to depends on infer type: ```ts function createSomeAction() { return { type: 'SOME_ACTION' }; } ``` or specify it: ```ts function createSomeAction(): FSA<undefined, undefined> { return { type: 'SOME_ACTION', payload: undefined, meta: undefined }; } ``` Is there a better solution? 🌷

Posted by unional on Nov 21, 2016, 6:17 AM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.4,500 issues0 comments

reliability (Software)

Treating `undefined` parameters as optional

unionalNov 21, 2016, 6:17 AM

tsc: 2.2.0-dev.20161116 Discover this behavior from https://github.com/acdlite/flux-standard-action/pull/45/commits/78a9065914b2ca4848dfba8fc0b47c54e2d0e319 This is the original code: ```ts export interface FSA<Payload, Meta> { ... payload?: Payload; ... } ``` However, this does not work well with type guard: ```ts 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: ```ts 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: ```ts function createSomeAction(): FSA<undefined, undefined> { // error: `payload` and `meta` are required return { type: 'SOME_ACTION' }; } ``` The workaround is to depends on infer type: ```ts function createSomeAction() { return { type: 'SOME_ACTION' }; } ``` or specify it: ```ts function createSomeAction(): FSA<undefined, undefined> { return { type: 'SOME_ACTION', payload: undefined, meta: undefined }; } ``` Is there a better solution? 🌷

Comments (0)

No comments yet. Start the discussion.

Comment on this issue