Effect CommunityEC
Effect Community3y ago
113 replies
Jules

Implementing Immutable Classes in TypeScript

How do you guys implement immutable classes? In Scala (sorry I'm primarily a Scala dev), we have a .copy method in each case class (a "record"), which allows us to make a copy of an instance and modify a field

Example:
case class Person(name: String, age: Int)
val jules = Person(name = "Jules", age = 34)
val olderJules = jules.copy(age = 35)

// result in memory:
// jules = Person("Jules", 34)
// olderJules = Person("Jules", 35)

How to achieve this .copy thing in TS?
Was this page helpful?