Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for SpreadElement node type when resulting array is ArrayLiteralExpression #2055

Open
gotenxds opened this issue Aug 31, 2024 · 2 comments

Comments

@gotenxds
Copy link

gotenxds commented Aug 31, 2024

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.

@arthurfiorette
Copy link
Collaborator

Hi, thanks for creating this issue! Want to open a PR for it? Remember to add unit tests :)

@gotenxds
Copy link
Author

gotenxds commented Sep 1, 2024

Hi @arthurfiorette, I'd love to but as I explained in the comment I tried to create a custom parser for this but I'm not sure how to tackle this one as it seems we get multiple nodes, one for each spread array instead of one node for the final array, if you have any idea how to tackle this one I'd be happy to finish it up including tests and open a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants