Socket
Socket
Sign inDemoInstall

@types/redux

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@types/redux

TypeScript definitions for Redux v3.3.1


Version published
Weekly downloads
151K
decreased by-1.76%
Maintainers
1
Weekly downloads
 
Created

What is @types/redux?

@types/redux provides TypeScript type definitions for the Redux library, enabling developers to use Redux with TypeScript more effectively. It includes type definitions for core Redux concepts such as actions, reducers, stores, and middleware.

What are @types/redux's main functionalities?

Defining Actions

This feature allows you to define the types of actions that can be dispatched in your Redux application. By defining action types, you can ensure that only valid actions are dispatched, reducing the risk of runtime errors.

interface IncrementAction {
  type: 'INCREMENT';
}

interface DecrementAction {
  type: 'DECREMENT';
}

type CounterAction = IncrementAction | DecrementAction;

Creating Reducers

This feature allows you to create type-safe reducers that handle state transitions based on the dispatched actions. By using TypeScript, you can ensure that your reducers handle all possible action types and return the correct state shape.

import { Reducer } from 'redux';

interface CounterState {
  count: number;
}

const initialState: CounterState = { count: 0 };

const counterReducer: Reducer<CounterState, CounterAction> = (state = initialState, action) => {
  switch (action.type) {
    case 'INCREMENT':
      return { count: state.count + 1 };
    case 'DECREMENT':
      return { count: state.count - 1 };
    default:
      return state;
  }
};

Configuring the Store

This feature allows you to create a Redux store with type-safe state and actions. By defining the state and action types, you can ensure that the store is correctly configured and that only valid actions are dispatched.

import { createStore, Store } from 'redux';

const store: Store<CounterState, CounterAction> = createStore(counterReducer);

Using Middleware

This feature allows you to apply middleware to your Redux store in a type-safe manner. Middleware can intercept and modify actions before they reach the reducer, enabling you to add custom behavior such as logging or asynchronous actions.

import { applyMiddleware, createStore, Store, Middleware } from 'redux';

const loggerMiddleware: Middleware<{}, CounterState> = store => next => action => {
  console.log('Dispatching:', action);
  let result = next(action);
  console.log('Next state:', store.getState());
  return result;
};

const store: Store<CounterState, CounterAction> = createStore(counterReducer, applyMiddleware(loggerMiddleware));

Other packages similar to @types/redux

FAQs

Package last updated on 14 Jul 2016

Did you know?

Socket

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.

Install

Related posts

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