I’m currently trying to make array type for my project
what I want to do is, whenever I give params to type, I want to make type base on it.
example:
type Dimension = Number;
type Point<Dimension, T extends Number[] = []> = Dimension extends T['length'] ? T : Point<Dimension, [...T, Number]>
type Boundary = ??? // I wanna declare array of length 2N(double of Point<N>)
//usage
const x = [1,2,3] as Point<3>
const xBoundary = [1,2,3,4,5,6] as Boundary<Point<3>>
//usage2
const y = [1,2,3,4] as Point<4>
const yBoundary = [1,2,3,4,5,6,7,8] as Boundary<Point<4>>
Is this possible??
When I declare Point type, I’ve used recurisve type definition, so I’ve tried to use double recursion? and tried to spread Point type for Boundary type
e.g)
type Boundary<Point, Dimension, T extends Number[]> = Dimension extends T['length'] ? T : Point<Dimension, [...Point, ...T, Number]>;
But didn’t worked, since typescript doesn’t recognize Point as spread element and compiler accepts all Number[] types.
2
Answers
Yes, you can. Here is an example.
I am assuming you want to have a tuple type with twice the length, this is possible like this.