是否可以像在 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