Created
April 13, 2025 05:31
-
-
Save avin-kavish/367f55a2aff653e80968b677c559fa97 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { immerable, produce } from 'immer' | |
type POJO<T> = { | |
[K in keyof T]: T[K] | |
} | |
class Car { | |
[immerable] = true | |
velocity = 0 | |
static fromJSON(data: POJO<Car>) { | |
return Object.assign(new Car, data) | |
} | |
accelerate() { | |
this.velocity = 50 | |
} | |
toJSON(): POJO<Car> { | |
return this // copy to pojo if you need | |
} | |
} | |
const prevState = new Car; | |
const newState = produce(prevState , c => c.accelerate()) | |
const cars = await api.cars.list().then(cars => cars.map(Car.fromJSON)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment