1

In Go I can do this:

type Country string
type City string

func findOnMap(country Country, city City) {
    //...
}

This guarantees that a country string will never be misused as a city. I use this a lot in Go. It is particularly useful for IDs since they are all ints but should never be used interchangeably.

I can't find out how to do the same thing in TypeScript. I tried the following:

type Country = string;

But this is a type alias, meaning that the types "string" and "Country" can be used interchangeably. This is precisely what I don't want.

My search has returned nothing and perusing the TS docs indicated this is not part of TS at all.

Did I miss anything? Are there any plans on adding this?

teleclimber
  • 318
  • 3
  • 7

0 Answers0