0

Scalaで行われているように、TypeScriptで厳密な型の値クラスを定義することは可能ですか?

タイプ エイリアスは、TypeScript の「コンパイラ」によって無視されるようです。

export type UserId = number;
export type CarId  = number;

const userId: UserId = 1

const findUser = (userId: UserId) => { ... }
findUser(userId) // OK

const findCar  = (carId: CarId) => { ... }
findCar(userId) // OK too! I would like to prevent such behavior

Scala では、値クラスを使用できます (厳密な型指定に加えて、より多くの利点があります)。

case class UserId(value: Int) extends AnyVal
4

1 に答える 1