CosmWasm / cosmwasm

Framework for building smart contracts in Wasm for the Cosmos SDK

Home Page:https://www.cosmwasm.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeScript type generation directly from Rust types

aumetra opened this issue · comments

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.

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)

commented

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.

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

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.

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).