Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nwtgck/ts-copyable-npm
Type-safe #copy() for TypeScript inspired by Scala case-class
https://github.com/nwtgck/ts-copyable-npm
npm npm-package typesafe typescript
Last synced: 25 days ago
JSON representation
Type-safe #copy() for TypeScript inspired by Scala case-class
- Host: GitHub
- URL: https://github.com/nwtgck/ts-copyable-npm
- Owner: nwtgck
- License: mit
- Created: 2017-08-09T06:37:35.000Z (over 7 years ago)
- Default Branch: develop
- Last Pushed: 2023-01-31T06:02:01.000Z (almost 2 years ago)
- Last Synced: 2024-10-01T16:09:21.746Z (about 1 month ago)
- Topics: npm, npm-package, typesafe, typescript
- Language: TypeScript
- Homepage:
- Size: 30.3 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ts-copyable
[![npm version](https://badge.fury.io/js/ts-copyable.svg)](https://badge.fury.io/js/ts-copyable) [![Build Status](https://travis-ci.org/nwtgck/ts-copyable-npm.svg?branch=master)](https://travis-ci.org/nwtgck/ts-copyable-npm)Type-safe .copy() for TypeScript inspired by Scala case-class
## How to install
```sh
$ npm install --save ts-copyable
```## How to use
### Before
```ts
class Person{
constructor(readonly name: string, readonly age: number){}
}
```### After
```ts
import { Copyable } from 'ts-copyable';
``````ts
class Person extends Copyable{
constructor(readonly name: string, readonly age: number){
super(Person);
}
}
```Then your `Person` class is copyable!
```ts
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?
```ts
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!)```