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

Generate struct from TS interface #1173

Open
aldo-roman opened this issue Mar 3, 2023 · 3 comments
Open

Generate struct from TS interface #1173

aldo-roman opened this issue Mar 3, 2023 · 3 comments

Comments

@aldo-roman
Copy link

Hi everyone
I have some complex Typescript interfaces from a 3rd party library and I would like to use superstruct to validate at runtime.

Is there a way I can generate a superstruct schema from a Typescript interface?

eg

// user.ts
interface User {
  id: number
  name: string
}

/* Run some node command */

// output/superstruct-user.ts
const User = type({
  id: number(),
  name: string(),
})
@JoniJnm
Copy link

JoniJnm commented Mar 9, 2023

Sounds good idea. But I think it only can be done creating a typescript plugin. Similar than kimamula/ts-transformer-keys does

@rvfakie
Copy link

rvfakie commented Apr 19, 2023

Wow it would be a nice feature. @ianstormtaylor is it possible to mark as a feature request?

@shellscape
Copy link

That would be a pretty heavy lift for this lib. While nice, it would involve walking a TypeScript AST, evaluating each node, and building a Node AST with something like acorn on the fly. As @JoniJnm mentioned, that would be better off a separate plugin.

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

4 participants