art-in/microcosm

check types of dispatched actions

art-in opened this issue · 0 comments

currently all action handler functions have type annotations, but we do not type-check calls.

this is a bit tricky, since action handler functions are not called directly, but through generic Store#dispatch function which looses param types.

store.dispatch('some-action-type', {payload: '123'}) // data object is not type-checked

One way to solve it.

It is possible to specify type of action parameter of Store#dispatch function not as generic Action object, but as list of all possible action forms, like

/**
 * @typedef {{type: 'some-action-type-1', data: {payload1: string}}} ActionType1
 * @typedef {{type: 'some-action-type-2', data: {payload2: string}}} ActionType2
 */

/**
 * @param {ActionType1|ActionType2} action
 */
dispatch(action) {}

But in this case we would want to share type definition of data object between data-param of target action handler function and action-param of Store#dispatch.

But import/export of jsdoc type definitions is not supported by typescript yet (microsoft/TypeScript#14377)