Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional generic constraint for FSA type property #113

Closed
wants to merge 3 commits into from

Conversation

fredrikhr
Copy link
Contributor

  • Make Payload type constaint optional (default to undefined) to allow shorthand FSA declarations for Actions without payloads.
  • Introduce new option Type type constraint (defaults to string) to allow shorthand FSA declarations for strong typing of the type property.

In TypeScript it is common to define actions with the type property declared as type: typeof ACTION_TYPE.

Thus, we might as well add an additional optional type constraint on the FSA type to express this.

For a Counter application example this makes it possible to change the following code:

import { FSA } from 'flux-standard-action';

const INCREMENT_COUNTER = 'INCREMENT_COUNTER';
type INCREMENT_COUNTER = typeof INCREMENT_COUNTER;
interface IncrementCounterAction extends FSA {
  type: INCREMENT_COUNTER;
}

to this:

import { FSA } from 'flux-standard-action';

const INCREMENT_COUNTER = 'INCREMENT_COUNTER';
type IncrementCounterAction = FSA<undefined, undefined, typeof INCREMENT_COUNTER>;

removing the need to re-declare the type property. The need to explicitly define the Meta type as undefined is unfortunate, but cannot be avoided if backwards compatability is to be maintained.

The type is optional (defaults to string) and is added as a 3rd constraint and thus maintains backwards compatability.

@fredrikhr fredrikhr changed the title Fsa overload Add optional generic constraint for FSA type property Nov 6, 2018
@JaKXz
Copy link
Contributor

JaKXz commented Nov 9, 2018

@couven92 can this be closed in favour of #114? I'm seeing a lot of similarities but I'll let you hit the button :)

@fredrikhr
Copy link
Contributor Author

@JaKXz yes, we can :)

@fredrikhr fredrikhr closed this Nov 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants