Socket
Socket
Sign inDemoInstall

ts-redux-actions

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-redux-actions - npm Package Compare versions

Comparing version 1.0.0-beta1 to 1.0.0-beta2

3

es5-commonjs/create-action.d.ts

@@ -6,3 +6,4 @@ export declare type ActionCreator<T extends string, P> = (() => {

payload: P;
meta?: any;
meta?: {};
error?: boolean;
});

@@ -9,0 +10,0 @@ export declare type TSActionCreator<AC, T extends string> = AC & {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var redux_1 = require("redux");
var store = redux_1.createStore(function () { return ({}); });
var _1 = require(".");

@@ -6,0 +4,0 @@ describe('Redux Utils', function () {

@@ -6,3 +6,4 @@ export declare type ActionCreator<T extends string, P> = (() => {

payload: P;
meta?: any;
meta?: {};
error?: boolean;
});

@@ -9,0 +10,0 @@ export declare type TSActionCreator<AC, T extends string> = AC & {

@@ -1,3 +0,1 @@

import { createStore } from 'redux';
var store = createStore(function () { return ({}); });
import { createAction } from '.';

@@ -4,0 +2,0 @@ describe('Redux Utils', function () {

@@ -6,3 +6,4 @@ export declare type ActionCreator<T extends string, P> = (() => {

payload: P;
meta?: any;
meta?: {};
error?: boolean;
});

@@ -9,0 +10,0 @@ export declare type TSActionCreator<AC, T extends string> = AC & {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const redux_1 = require("redux");
const store = redux_1.createStore(() => ({}));
const _1 = require(".");

@@ -6,0 +4,0 @@ describe('Redux Utils', () => {

{
"name": "ts-redux-actions",
"version": "1.0.0-beta1",
"version": "1.0.0-beta2",
"description": "Typed Redux Actions for TypeScript Projects",

@@ -24,4 +24,4 @@ "author": "Piotr Witek <piotrek.witek@gmail.com> (http://piotrwitek.github.io)",

"tsc:watch": "tsc -p . --noEmit -w",
"test": "jest --config jest.config.json",
"test:watch": "jest --config jest.config.json --watch",
"test": "jest --config jest.config.json ./src",
"test:watch": "jest --config jest.config.json ./src --watch",
"build": "yarn run build:commonjs & yarn run build:module & yarn run build:jsnext",

@@ -28,0 +28,0 @@ "build:commonjs": "rm -rf es5-commonjs/ && tsc -p . --outDir es5-commonjs/",

# TS Redux Actions
> Redux helpers for Type-safety (action types & action creators)
From now on no type errors will sneak in unnoticed through your action creators!
- Semantic Versioning

@@ -13,2 +16,3 @@ - No external dependencies

- [Motivation](#motivation)
- [Usage](#usage)
- [API](#api)

@@ -23,9 +27,108 @@ - [createAction](#createaction)

I wasn't satisfied with the API design in [redux-actions](https://redux-actions.js.org/). 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 will force you to do an extra effort for explicit type annotations and produce more boilerplate.
The most issues with `redux-actions` types are related to losing your function definition intellisense and named arguments in resulting action creator which for me is unacceptable.
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.
In examples part I will show you exactly the difference in API design but also between resulting (action creators) definitions coming from my solution and from `redux-actions` so you can clearly see the benefits.
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):
```ts
const increment = createAction('INCREMENT');
switch (action.type) {
case increment.type:
return state + 1;
...
...
default: return state;
}
```
---
## Usage
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:
- no payload
```ts
// 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 payload
```ts
// 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 payload and meta
```ts
// 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; };
// }
```
---
## API

@@ -32,0 +135,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc