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: 3 months 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 (almost 8 years ago)
- Default Branch: develop
- Last Pushed: 2023-01-31T06:02:01.000Z (over 2 years ago)
- Last Synced: 2025-04-13T10:12:54.183Z (3 months ago)
- Topics: npm, npm-package, typesafe, typescript
- Language: TypeScript
- Homepage:
- Size: 30.3 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ts-copyable
[](https://badge.fury.io/js/ts-copyable) [](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!)```