ts-json-schema-generator icon indicating copy to clipboard operation
ts-json-schema-generator copied to clipboard

Add support for `SpreadElement` node type when resulting array is `ArrayLiteralExpression`

Open gotenxds opened this issue 1 year ago • 2 comments

Hi, when create an Array literal type the parser works fine as the resulting node is ArrayLiteralExpression That is, when creating something like so:

const myArr = ['a', 'b'] as const;

export interface MyInter {
   prop: (typeof myArr)[number];
}

but when combining two or more literalArrays into a new literal array the subNodeParser is finding a a SpreadElement for each array first.

const myArr = ['a', 'b'] as const;
const mySecondArr = ['c', 'd'] as const;
const myThirdArr = [...myArr, ...mySecondArr] as const;

export interface MyInter {
   prop: (typeof myThirdArr)[number];
}

I tried to create a custom parser for this but I'm not sure how to, as it need to return a type for each node where in this case I would want to return a single type for the entire array and not a type per spread operation.

Note: An alternative would be to create a type like so

const myArr = ['a', 'b'] as const;
const mySecondArr = ['c', 'd'] as const;

type ThirdArr = (typeof myArr | typeof mySecondArr)[number];

const myThirdArr: ThirdArr = [...myArr, ...mySecondArr] as const;

export interface MyInter {
   prop: ThirdArr;
}

Which would work but will result in a anyOf instead of an enum which I need for my usecase.

gotenxds avatar Aug 31 '24 14:08 gotenxds