Skip to content

Instantly share code, notes, and snippets.

@avin-kavish
Created April 13, 2025 05:31
Show Gist options
  • Save avin-kavish/367f55a2aff653e80968b677c559fa97 to your computer and use it in GitHub Desktop.
Save avin-kavish/367f55a2aff653e80968b677c559fa97 to your computer and use it in GitHub Desktop.
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