TL;DR Today I learned how to mimic Scala-like value class in Typescript

Doing day to day scala I often use value classes to model the data like:

final case class UserId(value: String) extends AnyVal
final case class City(value: String) extends AnyVal

This is especially useful when you want to add some meaning to primitive types. It also makes the compiler help you to avoid passing irrelevant data to downstream methods.

Read more about value classes in the documentation

Today I’ve seen a good use case for similar technique in Typescript. It seems that you can kind of achieve it with parameter properties

Using parameter properties, the above example could be rewritten to:

class UserId {
  constructor(public readonly value: string) {}

class City {
  constructor(public readonly value: string) {}