crates/but-ts/README.md
but-ts generates TypeScript type declarations from JSON schemas registered in but-api.
This crate is the schema-to-TypeScript half of the @gitbutler/but-sdk declaration pipeline:
napi-rs writes function bindings (*Napi) and corresponding type declarations into index.d.ts.but-ts appends Rust-struct/enum-based type aliases into the same index.d.ts.Napi RS can generate TypeScript types for structs and enums as well, but it would require us to tag them with the #[napi] macro & re-export the type from but-napi.
but_api macro is the one marking the functions as napi export targets.This crate allows us to fully control how and where the types are generated.
TS-RS generates the types as part of tests, which is a bit awkward and we've had to add workarounds for them. This crate gives us full flexibility in how the types are generated, and to which output file.
In order to add the missing types in the Napi-RS-generated declaration file, we needed to be able to append the types to it. Which is something we couldn't easily do with TS-RS without breaking the current setup.
This crate is intended to be the single solution for TypeScript type generation.
but-api registers schema entries in but_api::schema.but-ts collects the registry (collect_all_schemas).--output).From repository root, use the but-sdk scripts:
pnpm --filter @gitbutler/but-sdk build:types
To regenerate both bindings and types in the recommended order:
pnpm --filter @gitbutler/but-sdk build
schemars::JsonSchema.crates/but-api/src/schema.rs with TypeSchemaEntry.pnpm --filter @gitbutler/but-sdk build:types.packages/but-sdk/src/generated/index.d.ts.After regenerating types, validate with:
pnpm --filter @gitbutler/but-sdk check
Optional runtime/type sanity validation:
pnpm --filter @gitbutler/but-sdk testTypes