Security News
Highlights from the 2024 Rails Community Survey
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
ts-redux-actions
Advanced tools
Redux helpers for Type-safety (action types & action creators)
From now on no type errors will sneak in unnoticed through your action creators!
main
module
jsnext:main
I wasn't satisfied with the API design in redux-actions. The reason was because of the specific API design (allowing multiple args for action creator) and separate payload, meta map functions which is not good for static typing in TypeScript. It doesn't allow for correct type inference and it will force you to do an extra effort for explicit type annotations and produce more boilerplate.
The most issues with redux-actions
types and similar solutions are related to losing your function definition inference and intellisense (named arguments and arity) in resulting action creator function which for me is unacceptable.
As a bonus you can use a convenient type
static property on every action creator for reducer switch case scenarios (working with narrowing of union types):
const increment = createAction('INCREMENT');
switch (action.type) {
case increment.type:
return state + 1;
...
...
default: return state;
}
To highlight the difference in API design and the benefits of "action creator" type inference found in this solution let me show you some usage examples:
// with redux-actions
const notify1 = createAction('NOTIFY')
// notice excess nullable properties "payload" and "error", "type" property widened to string
// const notify1: () => {
// type: string;
// payload: void | undefined;
// error: boolean | undefined;
// }
// with ts-redux-actions
const notify1 = createAction('NOTIFY')
// only what is expected, no nullables, with inferred literal type in type property!
// const notify1: () => {
// type: "NOTIFY";
// }
// with redux-actions
const notify2 = createAction('NOTIFY',
(username: string, message?: string) => ({
message: `${username}: ${message}`
})
)
// notice missing optional "message" argument, arg name changed to "t1", "type" property widened to string, and excess nullable properties
// const notify2: (t1: string) => {
// type: string;
// payload: { message: string; } | undefined;
// error: boolean | undefined;
// }
// with ts-redux-actions
const notify2 = createAction('NOTIFY', (type: 'NOTIFY') =>
(username: string, message?: string) => ({
type,
payload: { message: `${username}: ${message}` },
})
)
// still all good!
// const notify2: (username: string, message?: string | undefined) => {
// type: "NOTIFY";
// payload: { message: string; };
// }
// with redux-actions
const notify3 = createAction('NOTIFY',
(username: string, message?: string) => ({ message: `${username}: ${message}` }),
(username: string, message?: string) => ({ username, message })
)
// notice missing arguments arity and types, "type" property widened to string
// const notify2: (...args: any[]) => {
// type: string;
// payload: { message: string; } | undefined;
// meta: { username: string; message: string | undefined; };
// error: boolean | undefined;
// }
// with ts-redux-actions
const notify3 = createAction('NOTIFY', (type: 'NOTIFY') =>
(username: string, message?: string) => ({
type,
payload: { message: `${username}: ${message}` },
meta: { username, message },
}),
)
// inference working as expected and compiler will catch all those nasty bugs:
// const: notify: (username: string, message?: string | undefined) => {
// type: "NOTIFY";
// payload: { message: string; };
// meta: { username: string; message: string | undefined; };
// }
For more advanced usage scenarios please check use cases described in test specification
createAction(type, creatorFunction?)
type: string,
creatorFunction: (type: T) => (...args: any[]) => { type: T, payload?: P, meta?: M }
return: (...args: any[]) => { type: T, payload?: P, meta?: M }
Examples:
it('no payload', () => {
const increment = createAction('INCREMENT');
expect(increment()).toEqual({ type: 'INCREMENT' });
expect(increment.type).toBe('INCREMENT');
});
it('with payload', () => {
const add = createAction('ADD', (type: 'ADD') => (amount: number) =>
({ type, payload: amount }),
);
expect(add(10)).toEqual({ type: 'ADD', payload: 10 });
expect(add.type).toBe('ADD');
});
it('with payload and meta', () => {
const notify = createAction('NOTIFY', (type: 'NOTIFY') =>
(username: string, message: string) => ({
type,
payload: { message: `${username}: ${message}` },
meta: { username, message },
}),
);
expect(notify('Piotr', 'Hello!')).toEqual({
type: 'NOTIFY',
payload: { message: 'Piotr: Hello!' },
meta: { username: 'Piotr', message: 'Hello!' },
});
expect(notify.type).toBe('NOTIFY');
});
(WIP)
MIT License
Copyright (c) 2017 Piotr Witek piotrek.witek@gmail.com (http://piotrwitek.github.io)
FAQs
Typesafe Redux Action Creators for TypeScript
The npm package ts-redux-actions receives a total of 2 weekly downloads. As such, ts-redux-actions popularity was classified as not popular.
We found that ts-redux-actions demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.