Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I define a typescript type with a repeating structure?

I have a type that looks something like this:

type Location=`${number},${number};${number},${number};...`

Is there a Utility type like Repeat<T> that can do this for me? like this:

type Location=Repeat<`${number},${number};`>
like image 889
hyisYoo Avatar asked Aug 02 '26 09:08

hyisYoo


1 Answers

IT WORKS ONLY IN TS >=4.5

It is possible to create standalone type.

Please see this example:

type Coordinates = `${number},${number};`

type MAXIMUM_ALLOWED_BOUNDARY = 50

type Last<T extends string[]> = T extends [...infer _, infer Last] ? Last : never;

type ConcatPrevious<T extends any[]> = Last<T> extends string ? `${Last<T>}${Coordinates}` : never

type Mapped<
    N extends number,
    Result extends Array<unknown> = [Coordinates],
    > =
    (Result['length'] extends N
        ? Result
        : Mapped<N, [...Result, ConcatPrevious<Result>]>
    )


// type MyLocation = 
// | `${number},${number};` 
// | `${number},${number};${number},${number};` 
// | `${number},${number};${number},${number};${number},${number};` 
// | `${number},${number};${number},${number};${number},${number};${number},${number};` 
// | `${number},${number};${number},${number};${number},${number};${number},${number};${number},${number};` 
// | ... 44 more ... 
// | `${number},${number};${number},${number};${number},${number};${number},${number};${number},${number};${number},${number}; ....

type MyLocation = Mapped<MAXIMUM_ALLOWED_BOUNDARY>[number]

const myLocation1: MyLocation = '45,56;67,68;' // ok
const myLocation2: MyLocation = '45,56;67,68;1,2;3,4;5,6;7,8;9,10;' // ok
const myLocation3: MyLocation = '45,56;67,68;1,2;3,4;5,6;7,8;9,10,' // expected error

Playground

Mapped types is an utility type which represents a while loop. It iterates until length of Result will reach N. In other words Mapped<10> - will iterate 10 times. See this example in pure js:

const mapped = (N: number, Result: any[] = []): string => {
    if (N === Result.length) {
        return Result.join('')
    }
    
    const x = Math.random();
    const y = Math.random()
    return mapped(N, [...Result, `${x},${y};`])
}

It is hard to represent unions in js, thats why I have used join(''). I hope it is clear how it works.

If you want to increase MAXIMUM_ALLOWED_BOUNDARY to 500 it will heat your CPU so be careful.

As you might have noticed, it is impossible in type script to represent recursive pattern for type but it is possible to create big enough union.

Please keep in mind that there are some drawbacks of ${number} type. You are allowed to use numbers with leading zero like here:

const x: `${number}` = '01'.

Useful links:

  1. Here you can find an example how you can create a range of numbers using this pattern.

  2. Here you can find a PR where this feature was introduced

  3. Here and here you can find my articles which are related to this pattern

like image 167
captain-yossarian Avatar answered Aug 05 '26 08:08

captain-yossarian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!