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
- [BUG] Cowork (Windows): all Plan9 shares fail with "Plan9 mount failed: invalid argument" after Windows update KB5124008 (26200.9445) — uninstalling the KB fixe
- Feature Request: Auto-resume CLI session when usage limit resets
- Please deobfuscate the Python
- [Bug] MLX version of Gemma 4 26B fails to load: "Model type gemma4 not supported" (macOS)
- # 233 Developers Discussed Abandoning the Office Add-in Platform — An Open Letter on the Office.js Stability, Security & Trust Crisis
- [FEATURE] Allow users to configure which shell the Bash tool uses or inherit initial shell
Raise real-world problems, find people facing the same issue, and discuss solutions.4,500 issues0 comments
reliability (Software)
[BUG] Cowork (Windows): all Plan9 shares fail with "Plan9 mount failed: invalid argument" after Windows update KB5124008 (26200.9445) — uninstalling the KB fixe
Feature Request: Auto-resume CLI session when usage limit resets
Please deobfuscate the Python
[Bug] MLX version of Gemma 4 26B fails to load: "Model type gemma4 not supported" (macOS)
# 233 Developers Discussed Abandoning the Office Add-in Platform — An Open Letter on the Office.js Stability, Security & Trust Crisis
[FEATURE] Allow users to configure which shell the Bash tool uses or inherit initial shell
Admin Materials and Surfaces
AMD Support
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? 🌷
Comments (0)
No comments yet. Start the discussion.