
Security News
Node.js TSC Votes to Stop Distributing Corepack
Corepack will be phased out from future Node.js releases following a TSC vote.
@appnest/typed-actions
Advanced tools
A lightweight library to create and type check flux/redux actions in typescript
typed-actions
is a lightweight library to create and type check
flux/redux actions
in typescript.
The library doesn't come with a dispatcher, its only purpose is to type check actions.
:sparkles: Type magic - Define actions with the least amount of work on your side.
:mag: Small - No dependencies. Gzipped size: 483 B.
:vertical_traffic_light: Built in status - All statuses come with either a START
, SUCCESS
or FAILURE
status. This removes boilerplate for asynchronous action dispatching.
Using this library you avoid keeping track of uncomfortable many boilerplate interfaces in order to type check actions in typescript. Read the section without this library to see what typing hell you avoid.
This is based the following Typescript features, including features from 3.0: type guards, conditional types, union types, generic rest parameters and optional elements in tuple types.
All actions created using this library comes with a status
. This status can be either START
, SUCCESS
or FAILURE
. All actions must have a status because this become very handy when creating actions in an asynchronous flow. This way you avoid a lot of boilerplate (see without this library)
Let's start with a general example of how you would use this library.
Here we use an async action creator. You only need to define this action creator once for a given action, then it can be used to easily create and typecheck actions with different statuses: START
, SUCCESS
and FAILURE
. This is done using the methods start
, success
and failure
on the async action creator.
import { defaultAsyncActionCreator, isAction, Action } from "@appnest/typed-actions";
// 1: Make an action creator
const listUsers = defaultAsyncActionCreator<string[]>("LIST_USERS");
// 2: Create an action
const action = listUsers.success(["John", "Jane"]);
// { type: "LIST_USERS/SUCCESS", payload: ["John", "Jane"], id: "LIST_USERS", status: "SUCCESS" }
// 3: Dispatch the action any way you like
// 4: Handle the action in a store
function handler (action: Action) {
if (isAction(action, listUsers.success)) {
// Payload is type of "string[]" in this scope.
console.log(action.payload.join(", "));
} else if (isAction(action, listUsers.failure)) {
// Payload is type of "Error" in this scope.
console.log(`Something went wrong: ${action.payload.message}`)
}
}
In order to generate type safe actions you will first need to make an action creator. actionCreator
takes a generic parameter that becomes the type of the action payload.
import { actionCreator } from "@appnest/typed-actions";
const sendMsg = actionCreator<string>("SENG_MSG_ACTION");
The action creator can now be used to create actions with a payload of the specified type.
const action = sendMsg("Hello World");
Here's the content of this action.
{
type: "MY_ACTION/SUCCESS",
id: "MY_ACTION",
status: "SUCCESS",
payload: "Hello World",
meta: undefined
}
You will notice that the status is "SUCCESS". All actions must have a status becauses this become very handy when spawning actions in an asynchronous flow. "SUCCESS" is the default status.
The action can be dispatched any way you like (for example redux store.dispatch(action)
). This library is only build for creating and typechecking actions, not for dispatching them.
Now you are ready to check the action type. This is done using the isAction
function.
import { isAction, Action } from "@appnest/typed-actions";
function handler (action: Action) {
// Check if "action" is of type "sendMsg"
if (isAction(action, sendMsg)) {
// Type of "payload" is "string" in this scope
console.log(action.payload);
}
}
Here is an example of what you would have to do if you did not use this library. Typed actions are achieved using discriminated unions. In addition it's difficult to automate dispatching eg. failure
actions because the status
is baked into the type.
// 1: Make a bunch of interfaces
interface ListUserStartAction {
type: "LIST_USER/START";
}
interface ListUserSuccessAction {
type: "LIST_USER/SUCCESS";
payload: string[];
}
interface ListUserFailureAction {
type: "LIST_USER/FAILURE";
payload: Error;
}
type Action = ListUserStartAction | ListUserSuccessAction | ListUserFailureAction;
// 2: Create an action
const action: Action = {
type: "LIST_USER/SUCCESS",
payload: ["John", "Jane"]
};
// 3: Dispatch the action
// 4: Handle the action
function handler (action: Action) {
switch (action.type) {
case "LIST_USER/SUCCESS":
// Payload is type of "string[]" in this scope.
console.log(action.payload.join(", "));
break;
case "LIST_USER/FAILURE":
// Payload is type of "Error" in this scope.
console.log(`Something went wrong: ${action.payload.message}`);
break;
}
}
FAQs
A lightweight library to create and type check flux/redux actions in typescript
The npm package @appnest/typed-actions receives a total of 42 weekly downloads. As such, @appnest/typed-actions popularity was classified as not popular.
We found that @appnest/typed-actions demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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
Corepack will be phased out from future Node.js releases following a TSC vote.
Research
Security News
Research uncovers Black Basta's plans to exploit package registries for ransomware delivery alongside evidence of similar attacks already targeting open source ecosystems.
Security News
Oxlint's beta release introduces 500+ built-in linting rules while delivering twice the speed of previous versions, with future support planned for custom plugins and improved IDE integration.