https://github.com/myty/immutable-record
The sole purpose of this immutable record is to simply act as a class factory for immutable types. For this implementation, it piggybacks off immer and adds a 'with' method to the record class.
https://github.com/myty/immutable-record
deno immer typescript
Last synced: 4 months ago
JSON representation
The sole purpose of this immutable record is to simply act as a class factory for immutable types. For this implementation, it piggybacks off immer and adds a 'with' method to the record class.
- Host: GitHub
- URL: https://github.com/myty/immutable-record
- Owner: myty
- License: mit
- Created: 2021-02-14T19:22:46.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-11-05T04:15:35.000Z (over 1 year ago)
- Last Synced: 2025-11-17T15:09:09.645Z (7 months ago)
- Topics: deno, immer, typescript
- Language: TypeScript
- Homepage:
- Size: 171 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Introduction
Built on `immer`, immutable recorda are class factories for immutable types.
## Deno
```sh
deno add jsr:@myty/immutable-record
```
## Node
```sh
npm install @myty/immutable-record
```
## Usage
```typescript
import { ImmutableConstructor, ImmutableRecord, ImmutableWith } from "@myty/immutable-record";
interface TestDataInterface {
testNumber: number;
testString: string;
optionalString?: string;
}
const defaultValues: TestDataInterface = {
testNumber: 1,
testString: "test",
};
class TestClass extends ImmutableRecord(defaultValues) {
withTestNumber(testNumber: number) {
return this.with({ testNumber });
}
}
const value = new TestClass();
// { testNumber: 1, testString: 'test' }
const newValue = value.with({ testNumber: 2, optionalString: "test-optional" });
// { testNumber: 2, testString: 'test', optionalString: 'test-optional' }
```
### Processing Nested Classes
```typescript
interface TestParentClassInterface {
data: TestDataInterface;
}
class TestParentClass extends ImmutableRecord(
{ data: new TestClass() },
(values) => {
let { data } = values;
if (!(data instanceof TestClass)) {
data = new TestClass(data);
}
return { ...values, data };
},
) {}
const value = new TestParentClass({
data: { testNumber, testString },
});
// value.data instanceof TestClass === true
const value = new TestParentClass().with({
data: { testNumber, testString },
});
// value.data instanceof TestClass === true
```