ts-copyable
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

ts-copyable

npm version Build Status Greenkeeper badge

Type-safe .copy() for TypeScript inspired by Scala case-class

How to install

$ npm install --save ts-copyable

How to use

Before

class Person{
    constructor(readonly name: string, readonly age: number){
 
    }
}

After

import { Copyable } from 'ts-copyable';
class Person extends Copyable<Person>{
    constructor(readonly name: string, readonly age: number){
        super(Person);
    }
}

Then your Person class is copyable!

const p1 = new Person("jack", 10);
 
p1.copy({age: 2});
// => Person("jack", 2)
 
p1.mapCopy({age: prev => prev+1});
// => Person("jack", 11)

How about type-safety?

const p1 = new Person("jack", 10);
 
p1.copy({age: "abc"});
// Compile error (GOOD!)
 
p1.copy({somethingElse: 99});
// Compile error (GOOD!)
 
p1.mapCopy({age: prev => prev+"abc"});
// Compile error (GOOD!)
 

Readme

Keywords

Package Sidebar

Install

npm i ts-copyable

Weekly Downloads

0

Version

0.1.0

License

MIT

Unpacked Size

10.3 kB

Total Files

11

Last publish

Collaborators

  • nwtgck