superstruct icon indicating copy to clipboard operation
superstruct copied to clipboard

Generate struct from TS interface

Open aldo-roman opened this issue 2 years ago • 3 comments

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(),
})

aldo-roman avatar Mar 03 '23 09:03 aldo-roman

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

JoniJnm avatar Mar 09 '23 20:03 JoniJnm

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

rvfakie avatar Apr 19 '23 18:04 rvfakie

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.

shellscape avatar Jun 10 '23 15:06 shellscape