Hacker News new | ask | show | jobs
by presz 325 days ago
In TypeScript you can enable this by using BrandedTypes like this:

  type UserId = string & { readonly __tag: unique symbol };

In Python you can use `NewType` from the typing module:

  from typing import NewType
  from uuid import UUID

  UserId = NewType("UserId", UUID)
1 comments

In Python 3.12 syntax, you can use

    type UserIs = UUID
`type UserId = UUID` creates a TypeAlias, not the same thing (from a type checker's point of view) as a NewType [1].

[1] https://typing.python.org/en/latest/spec/aliases.html