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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: generate to JavaScript with JSDoc types #39

Open
croconut opened this issue Mar 6, 2024 · 7 comments 路 May be fixed by #41
Open

feature request: generate to JavaScript with JSDoc types #39

croconut opened this issue Mar 6, 2024 · 7 comments 路 May be fixed by #41

Comments

@croconut
Copy link

croconut commented Mar 6, 2024

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
Copy link
Owner

xddq commented Mar 6, 2024

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
Copy link
Author

croconut commented Mar 6, 2024

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 croconut changed the title feature request: generate to a JSDoc file feature request: generate to JavaScript with JSDoc types Mar 6, 2024
@croconut
Copy link
Author

croconut commented Mar 6, 2024

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
Copy link
Owner

xddq commented Mar 6, 2024

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
Copy link
Author

croconut commented Mar 6, 2024

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

@xddq
Copy link
Owner

xddq commented Mar 17, 2024

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
Copy link
Author

Oh lol sure

@croconut croconut linked a pull request Mar 18, 2024 that will close this issue
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

Successfully merging a pull request may close this issue.

2 participants