TypeScript Mapped Types & Utility Collection

3.0.0-rc.3 · active · verified Sun Apr 19

react-redux-typescript provides a robust collection of advanced TypeScript utility types, primarily focusing on 'Mapped Types' for complex object and key manipulation. As of version 3.11.0, it offers types like `DiffKeys`, `Omit`, `Assign`, `Overwrite`, `DeepReadonly`, `Optional`, `Required`, `Mutable`, and `UnionToIntersection`. It also includes a utility function `getReturnOfExpression` for inferring function return types. The library is actively maintained with frequent minor releases, though its name can be misleading as the React/Redux-specific parts (re-exports from `typesafe-actions`) are deprecated and slated for removal in future major versions. A key differentiator is its commitment to thorough type correctness testing, zero third-party runtime dependencies for its core utility types, and providing separate bundles for various module workflows (CommonJS, ES Module).

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates `Omit`, `Assign`, and `DeepReadonly` utility types for manipulating object shapes and immutability.

import { Omit, Assign, DeepReadonly } from 'react-redux-typescript';

interface UserProfile {
  id: string;
  name: string;
  email: string;
  settings: {
    theme: 'dark' | 'light';
    notifications: boolean
  };
  createdAt: Date;
}

interface PartialUpdate {
  name?: string;
  email?: string;
  updatedAt: Date;
}

// Example 1: Omit properties from a type
type UserWithoutSensitiveInfo = Omit<UserProfile, 'email' | 'createdAt'>;
// Expected: { id: string; name: string; settings: { theme: 'dark' | 'light'; notifications: boolean }; }

const user1: UserWithoutSensitiveInfo = {
  id: '123',
  name: 'Alice',
  settings: { theme: 'dark', notifications: true }
};

// Example 2: Assign (merge) properties, overwriting if they exist
type MergedProfile = Assign<UserProfile, PartialUpdate>;
// Expected: { id: string; name: string; email: string; settings: { theme: 'dark' | 'light'; notifications: boolean }; createdAt: Date; updatedAt: Date; }

const user2: MergedProfile = {
  id: '456',
  name: 'Bob',
  email: 'bob@example.com',
  settings: { theme: 'light', notifications: false },
  createdAt: new Date(),
  updatedAt: new Date()
};

// Example 3: Make all properties and nested properties readonly
type ReadonlyUserProfile = DeepReadonly<UserProfile>;

const readonlyUser: ReadonlyUserProfile = {
  id: '789',
  name: 'Charlie',
  email: 'charlie@example.com',
  settings: {
    theme: 'light',
    notifications: true
  },
  createdAt: new Date()
};

// readonlyUser.name = 'David'; // This line would cause a TypeScript error

console.log('User without sensitive info:', user1);
console.log('Merged profile:', user2);
console.log('Readonly user (check types):', readonlyUser);

view raw JSON →