xddq / schema2typebox

Creating TypeBox code from JSON schemas
MIT License
66 stars 15 forks source link

feature request: generate to JavaScript with JSDoc types #39

Open croconut opened 8 months ago

croconut commented 8 months ago

Library version:

1.7.1

JSON schema version: draft-04

I am willing to contribute to fix the issue πŸ’š

The current behavior

Generates to typescript.

The expected behavior

Identify the syntax to use based on the output file extension when writing. -> .cjs & .mjs & .js would be javascript compliant code, while ts would be typescript.

xddq commented 8 months ago

Hey @croconut !

Thanks for creating the issues. I guess this is the first one you want to target. What exactly do you want to do here?

a) generate js code that uses typebox (my assumption based on issue body) b) generate js doc only containing jsdoc which contains typings that match the ts typings. (my assumption based on issue title)

Or perhaps something completely different..?

Anyway, I would be down to have either of this added. Do you have any initial ideas for implementing it? I will state my thoughts as well once I know what the goal will be.

croconut commented 8 months ago

Thanks for writing this library! :)

So I'm looking for a js file that uses jsdoc annotated types.

It looks something like the below to define a TS type with JSDocs.


/** @typedef {import('@sinclair/typebox').Static<typeof T>} TType */
croconut commented 8 months ago

I would need a different name for the schema's type when using this from commonjs afaict, so right now I'm using TType in my codebase.

xddq commented 8 months ago

Alright. I hope I did now understand what the goal is. To generate a .js file with the typebox code which contains jsdoc comments of the types when the value given for the "output" argument of the cli has a javascripty file ending. Correct?

If you want feedback on your approach let me know on a high level how you want to go at this. Otherwise, feel free to get going and submit a PR : )

Just a thought I had on this now (might be bad or wrong):

A decent strategy could even be to "postprocess" the generated typescript code. If I am seeing it correctly, all stuff like export const TypeName = Type.Object({...}) is valid javascript code. Therefore all that is left is to find and convert are the type definitions, e.g. export type TypeName = Static<typeof TypeName> to something like you already mentioned such as /** @typedef {import('@sinclair/typebox').Static<typeof TypeName>} TypeName */

croconut commented 8 months ago

LGTM, although you'd also need to remove type imports

xddq commented 8 months ago

Hey @croconut ,

just wondering, are there any updates on this? I did assume you will give this a shot and send a PR for this. Is this assumption correct?

croconut commented 8 months ago

Oh lol sure