GitXplorerGitXplorer
a

dataclass

public
185 stars
6 forks
2 issues

Commits

List of commits on branch master.
Unverified
754b8b3cf9da13485f4143ba35f94d69f9affd08

runtime type checking, part 1

aalexeyraspopov committed 9 months ago
Unverified
9c6c924d2a8b71dbee12f1317767a0a150aa39c6

use opaque generic type to mark required fields

aalexeyraspopov committed 9 months ago
Unverified
741920e529e9787a2a4759a8bd5d4504c81a85ae

flow: almost feature parity with typescript

aalexeyraspopov committed 9 months ago
Unverified
6ab3fb5fd1ae12605ddf1aac3f023f41dcddffa6

use Object.is() for equality check

aalexeyraspopov committed 9 months ago
Unverified
60c98b83fc9a282bd094ec28c4e6666570c58983

ts: require optional fields in create() and mark instances as Required

aalexeyraspopov committed 9 months ago
Unverified
d2be8b5ac531de80f2fd49d154a78d58a67ac61f

-deps

aalexeyraspopov committed a year ago

README

The README file for this repository.

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.

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 in Kotlin (also Case Classes in Scala):

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 in Python:

from dataclasses import dataclass, replace

@dataclass
class User:
  name: str = "Anonymous"
  age: int = 0

user = 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.

The project is licensed under the ISC license.