1.0.0 • Published 5 years ago

remutate v1.0.0

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

remutate

Library for type safe mutations of immutable objects in typescript

npm install remutate

Motivation:

Using spread operators to mutate immutable objects in typescript in some cases may not be handled properly by compiler. Let's consider the following example:

interface User {
    firstName: string;
    lastName: string;
    age: number;
}

const changeUserName: (user: User) => User = user =>  {
    const name = 'New User Name';
    return { ...user, name }; // No compilation error
}

To help compiler just import mutate and use it instead of spread operators.

import { mutate } from 'remutate';

interface User {
    firstName: string;
    lastName: string;
    age: number;
}

const changeUserNameSafe: (user: User) => User = user => {
    const name = 'New User Name';
    return mutate(user, {name }); // Compilation error
}