the-dr-lazy / deox

Functional Type-safe Flux Standard Utilities
https://deox.js.org
MIT License
206 stars 12 forks source link

Create polymorphic (generic) action creator #149

Closed anilanar closed 4 years ago

anilanar commented 4 years ago

I have created an action such as:

export const foo = createAction(
  'FOO',
  resolve => <S extends string>(s: S) => resolve(s),
);

The action type is correct, the output function has the correct generic constraints and types.

However, using such an action creator with createReducer causes type of action to be never in the handler function.

This is a simplified version of the actual use case. We really do use polymorphic actions in a useful way.

the-dr-lazy commented 4 years ago

This looks like as a design limitation in TypeScript.

I don't see anyway to infer polymorphic action type.

declare const foo: <S extends string>(s: S) => { type: 'FOO', payload: S }

type Action = ReturnType<typeof foo> //=> any

@anilanar do you have any idea?

anilanar commented 4 years ago

You are right @the-dr-lazy, it's currently not possible.