Typescript Nominal Types using Symbols

Sometimes nominal typing is important. Because typescript is structural, it is not easy to create nominal types. But there are some hacks. Some people will intersect the type with Enums, and some with an object with a specific property name that won’t repeat itself easily. As symbols are unique, it is a good choice to use an unique symbol as the key for a field that will break the structure. And another advantage is that the IDE won’t sugest that field for you when you are coding. So, without wasting anyone’s time, here is the solution:

You can export the Nominal type and create as many unique types as you want. If you want to be even more strict. You can pass another unique symbol as the SymType and it wont repeat itself along your code.

--

--