Can I define, for example, âwith an id consisting of alphanumeric characters onlyâ in TypeScript type information? src I saw that, and I wondered how to do that, so I looked it up and made a note of it.
I think this kind of need is, in essence, using existing String as a data structure in Structural typing TypeScript, but restricting the range of values, and wanting to give a different name to the set of values, or Nominal Typing.
I looked it up in Nominal Typing and found it right away. Nominal Typing - TypeScript Deep Dive
It is simple enough to introduce a member for type distinction and to eliminate structural incompatibility. I used this explanation to create an odd-numbered type.
ts
Some people may find it âtougher than expectedâ.
odd = 1
results in a type error.
As in this example, if you think âitâs a number, but itâs obviously odd,â you can just assert it.
If this ASSERT is incorrect, an assertion error will occur at runtime.
This page is auto-translated from /nishio/TypeScriptă§ć„æ°ć using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. Iâm very happy to spread my thought to non-Japanese readers.