Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alexeyraspopov/dataclass
Data classes for TypeScript & JavaScript
https://github.com/alexeyraspopov/dataclass
data-class data-structures dataclass immutable javascript typescript value-object zero-dependency
Last synced: about 3 hours ago
JSON representation
Data classes for TypeScript & JavaScript
- Host: GitHub
- URL: https://github.com/alexeyraspopov/dataclass
- Owner: alexeyraspopov
- License: isc
- Created: 2017-06-17T15:26:00.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-04-22T13:41:57.000Z (10 months ago)
- Last Synced: 2025-01-25T17:08:12.404Z (7 days ago)
- Topics: data-class, data-structures, dataclass, immutable, javascript, typescript, value-object, zero-dependency
- Language: JavaScript
- Homepage: https://dataclass.js.org
- Size: 659 KB
- Stars: 185
- Watchers: 2
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- awesome-github-repos - alexeyraspopov/dataclass - Data classes for TypeScript & JavaScript (JavaScript)
README
# dataclass
npm install dataclass
Syntax sugar that leverages the power of available type systems in TypeScript and JavaScript to
provide an effortless way for defining value objects that are immutable and persistent.Read full docs [on the website](https://dataclass.js.org).
```ts
import { Data } from "dataclass";class User extends Data {
name: string = "Anon";
age: number = 25;
}let user = User.create({ name: "Liza", age: 23 });
// > User { name: "Liza", age: 23 }let updated = user.copy({ name: "Ann" });
// > User { name: "Ann", age: 23 }let isEqual = user.equals(updated);
// > false
```## Prior Art
The implemented concept is heavily inspired by Scala and Kotlin. Both languages have the
implementation of data classes as a part of their syntax and share similar APIs.See [Data Classes](https://kotlinlang.org/docs/reference/data-classes.html) in Kotlin (also
[Case Classes](https://docs.scala-lang.org/tour/case-classes.html) in Scala):```kotlin
data class User(val name: String = "Anonymous", val age: Int = 0)val user = User(name = "Liza", age = 23)
val updated = user.copy(name = "Ann")user.equals(updated)
```And [Data Classes](https://docs.python.org/3/library/dataclasses.html) in Python:
```python
from dataclasses import dataclass, replace@dataclass
class User:
name: str = "Anonymous"
age: int = 0user = User(name="Liza", age=23)
updated = replace(user, name="Ann")user == updated
```## Contributing
The project is opened for any contributions (features, updates, fixes, etc). If you're interested,
please check
[the contributing guidelines](https://github.com/alexeyraspopov/dataclass/blob/master/CONTRIBUTING.md).The project is licensed under the
[ISC](https://github.com/alexeyraspopov/dataclass/blob/master/LICENSE) license.