0

I'm working on a system where all ids are represented by strings, but that has an issue where you can get different data's ids mixed up (eg a cat and dog each have an id, but with just strings you could set cat.id = dog.id which would be incorrect).

I know I can make a type alias to make this clearer (eg, CatId and DogId will at least be clearer), but this still doesn't prevent assigning them.

Is there any way to make a new type that cannot be directly assigned from another type, even if they're both strings internally? (without resorting to wrapper objects)

zacaj
  • 1,987
  • 1
  • 19
  • 39
  • Does this solve your issue? https://stackoverflow.com/questions/12897742/how-do-you-specify-that-a-class-property-is-an-integer/50521248#50521248 – derkoe Nov 25 '19 at 17:35
  • @koe that seems to, yes! a bit cumbersome but the types seem to be erroring when they should – zacaj Nov 25 '19 at 17:43
  • Does this answer your question? [Is there a way to create nominal types in TypeScript that extend primitive types?](https://stackoverflow.com/questions/26810574/is-there-a-way-to-create-nominal-types-in-typescript-that-extend-primitive-types) – derkoe Nov 25 '19 at 17:58
  • Without wrapper not possible. – Maciej Sikora Nov 25 '19 at 22:09

0 Answers0