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

Only compile interface and types without runtime code #50

Open
jvgomg opened this issue Aug 6, 2024 · 3 comments
Open

Only compile interface and types without runtime code #50

jvgomg opened this issue Aug 6, 2024 · 3 comments

Comments

@jvgomg
Copy link

jvgomg commented Aug 6, 2024

I am interested in using Pkl to share static typings between Go and Typescript. I don't think I need the "evaluator" part of the code as I'm just interested in consuming the export interface Config code.

Is there a way to use pkl-gen-typescript and omit the imports, loadFromPath function and load function?

I can see there is a --settings-file option but snooping around generator-settings.pkl I cannot see anything relevant.

Maybe it makes sense to add a "definitions" mode which only generates .d.ts files? 🤔

Many thanks!

@jasongwartz
Copy link
Contributor

Thanks for letting us know about your interest, @jvgreenaway! Feel free to come join us in the Pkl community Discord if you want to chat in more detail (both @jackkleeman and I are there, and there's a #bindings-typescript channel).

This is a great question and use-case, and one we've gotten once or twice before. I've also had times where I wanted to use Pkl as a 'lingua franca' for type definitions, that generates types for multiple languages, OpenAPI, etc. And I'm open to the idea of a "type definitions only mode" that doesn't include the evaluator imports.

The one issue we've had when discussing this before is that the generated types from pkl-gen-typescript are designed based on constraints that exist at runtime when evaluating Pkl code. For example, a Pkl Mapping or Map will type-gen an ES6 Map, not a Record; that's because Pkl allows any hashable object as a Mapping/Map key, not just strings. That can be annoying if you were hoping to access properties of the object using a subscript or dot-notation. When we discussed this last, I believe we came to the conclusion that it was not possible to satisfy both cases, so we stuck with type-generating an ES6 Map as an option that works correctly in both cases, and is slightly less ergonomic in the "using only the type generator but not the evaluator" case.

Another option if this constraint is unsatisfactory is generating a JSON Schema or OpenAPI spec from your Pkl module, and using code-generators with more customisation options.

If that constraint isn't a blocking issue for you, I'll talk over the idea in Discord and see if we think this is a reasonable addition.

@jvgomg
Copy link
Author

jvgomg commented Aug 6, 2024

@jasongwartz thank you very much for such an informative and helpful response. I am going to play with the the Mapping and Map types to fully understand what you are saying.

My use case is primarily to use the typealias type to generate union types, so that I have shared enums.

I'll come visit the Discord!

@jvgomg
Copy link
Author

jvgomg commented Aug 6, 2024

Another update my end - I am going to experiment with https://github.com/gzuidhof/tygo. This could fulfil my use case.

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