0

How can I kind of narrow down the usages in the code of a string that can hold any value but has a semantic. E.g. a special ID or an email.

E.g. something like that:

type EMail = string;
type OrderID = string;

getUserEmail(): EMail;
getOrderById(id: OrderID): Order;

I can declare types but this still compiles:

getOrderById(getUserEmail());  // Does not throw a compiler error.

Is there a way to enforce that this is not possible?

jaw
  • 932
  • 2
  • 10
  • 24
  • 1
    You can check [this](https://basarat.gitbook.io/typescript/main-1/nominaltyping) out – Christian Vincenzo Traina May 31 '22 at 09:49
  • Thanks, I think both the duplicate reply as well as this doc page addresses my point. I justs didn't know how this concept is called, so I didn't know "nominal typing" or "branded types". – jaw May 31 '22 at 09:54

0 Answers0