TypeScript string alias repeated substring

Viewed 27

I want to create an alias for WKT strings. So I declared a type like below:

type WicketCoordinates = `${number} ${number}` | `${number} ${number} ${number}`;

Now it is simple to build alias for POINT. But when it comes to other geometries like LineString I need an string which is a comma separated of n-times repeats of type WicketCoordinates.

By that I mean I want a type that can accept all of these below:

let a = "12 34"; // One single WicketCoordinates
let b = "12 34,56 78"; // A combination of two WicketCoordinates separated by comma
let c = "12 34,56 78,90 12"; // A combination of three WicketCoordinates separated by comma
let d = "12 34,56 78, ... ,90 12"; // A combination of n-times WicketCoordinates separated by comma

So I tried declaring this type:

type WicketCommaSeperatedCoordinates = WicketCoordinates | `${WicketCoordinates},${WicketCoordinates}` | `${WicketCommaSeperatedCoordinates},${WicketCommaSeperatedCoordinates}`;

but that comes with circularly references itself error. So I tried this type:

type WicketCommaSeperatedCoordinates = {
  value:
    | WicketCoordinates
    | `${WicketCoordinates},${WicketCoordinates}`
    | `${WicketCommaSeperatedCoordinates['value']},${WicketCommaSeperatedCoordinates['value']}`;
};

But that also comes with referenced directly or indirectly in its own type annotation error.

The absolute question: Is there any way in typescript to declare a type which is an alias string made of n-times repeats of a substring?

0 Answers
Related