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

TypeScript type generation directly from Rust types #2093

Open
aumetra opened this issue Apr 9, 2024 · 6 comments
Open

TypeScript type generation directly from Rust types #2093

aumetra opened this issue Apr 9, 2024 · 6 comments
Assignees
Labels
enhancement New feature or request schema cosmwasm-schema crate
Milestone

Comments

@aumetra
Copy link
Member

aumetra commented Apr 9, 2024

At the moment we generate the TypeScript types by outputting JSON Schema files and then converting them into TypeScript types.
This not only loses some type information but it also complicates the setup.

Since TypeScript is pretty much the only language that clients are written in, we decided through internal discussion that phasing out JSON Schemas in favour of generating TypeScript types is a worthwhile endeavour.

This drops our dependence on the schemars crate in favour of using the ts-rs crate to generate these types.

@aumetra aumetra added enhancement New feature or request schema cosmwasm-schema crate labels Apr 9, 2024
@aumetra aumetra added this to the 2.1.0 milestone Apr 9, 2024
@aumetra aumetra modified the milestones: 2.1.0, 2.2.0 Apr 19, 2024
@aumetra
Copy link
Member Author

aumetra commented May 3, 2024

Just adding this here to not forget: apparently some people are not using the cw_serde macro to generate their types (ref: https://twitter.com/kromssten/status/1747287780214935754), so simply changing the generate_api macro to unconditionally emit TS definitions won't cut it. Since that would break these users.

Technically we have two options:

  1. Call it acceptable breakage and say anything besides the cw_serde macro isn't supported
  2. Implement a specialization hack based on auto-ref (see: https://docs.rs/spez/latest/spez/)

I think the second option would probably be nicer for our consumers since it doesn't break anything for them, but it would increase the macro complexity a bit (but I don't think to a big enough degree to really matter)

@kakucodes
Copy link

Since TypeScript is pretty much the only language that clients are written in, we decided through internal discussion that phasing out JSON Schemas in favour of generating TypeScript types is a worthwhile endeavour.

I'm not certain that this is a good assumption. cosmpy and go-codegen speak to different ecosystems where code generation to non-ts environments are used.

@aumetra
Copy link
Member Author

aumetra commented Jun 3, 2024

I'm not certain that this is a good assumption. cosmpy and go-codegen speak to different ecosystems where code generation to non-ts environments are used.

@kakucodes that's actually a good point. Especially after looking at things shown off at AwesomWasm that leveraged JSON Schemas.

In internal discussions we were talking about different languages parsing TypeScript to generate types from that. But not sure how viable that is in practice.

Thoughts? @webmaster128 @chipshort

@webmaster128
Copy link
Member

After speaking to both Dan and Serdar (who build ts-codegen/go-codegen) I think we should additionally dump an internal machine-readable intermediate represenation that other tools can laverage. So the generation looks like

Rust source code -------> JSON dump -------> TypeScript types

The advantage of this JSON dump over JSON Schema is that it does not loose information and can be tailored to our needs.

@webmaster128
Copy link
Member

Since that would break these users.

I would plan for a long lasting transition period where JSON schema remains untouched as long as the users consider it useful. A different type exporter can then be used in addition to that (e.g. by a second function that runs as part of cargo schema).

@webmaster128
Copy link
Member

In #451 we hit the JSON Schema limitation that an enum must have at least one element. This makes it improssible to describe a type that cannot be instantiated. For the new schemas, could we have a representation of that? We have cosmwasm_std::Never already and need a valid interface definition for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request schema cosmwasm-schema crate
Projects
None yet
Development

No branches or pull requests

4 participants
@webmaster128 @aumetra @kakucodes and others