Skip to content

Feature flags

escritorio-gustavo edited this page Feb 21, 2024 · 2 revisions

Feature flags

ts-rs has feature flags to help you with serde compatibility, formatting the output TS files, using ES Modules style imports and implementing TS for types of some popular libraries.

Functionality features

These features change the behavior of ts-rs in certain ways

serde-compat (default)

Enables serde compatibility. This means ts-rs will make sure that serde attributes also affect TS type generation. Compatible attributes are:

  • #[serde(rename = "...")]
  • #[serde(rename_all = "...")]
  • #[serde(rename_all_fields = "...")]
  • #[serde(tag = "...")]
  • #[serde(tag = "...", content = "...")]
  • #[serde(untagged)] (Both on the enum itself and on individual variants)
  • #[serde(flatten)]
  • #[serde(default = "...")]
  • #[serde(tag = "...")]
  • #[serde(skip)]

This reduces code duplication, since you don't need to apply both #[ts(...)] and #[serde(...)] for the same attribute, #[serde(...)] is enough.

format

Formats the resulting TS code using dprint-plugin-typescript

no-serde-warnings

In serde-compat mode, using a serde attribute not listed as compatible, e.g. #[serde(with = "...")] will generate a warning. You can disable these warnings with this flag

import-esm

The ES Modules spec requires import statements to include the imported file's extension. This flag will append .js to generated import paths

Implementing TS for external types

Due to Rust's orphan rule, you cannot apply a foreign trait (such as TS) to a foreign type (such as chrono::NaiveDate). The following features implement TS for types of some popular crates inside ts-rs to help you get around this issue.

If you need a type not implemented, use #[ts(as = "...")] or submit a PR to add a feature that implements it. Keep in mind that if you choose to open a PR, you need to implement TS for all relevant types of the targeted crate as to avoid surprising users (imagine having chrono::NaiveDate implement TS but not chrono::NaiveDateTime!) as well as write the appropriate unit tests.

chrono-impl

bigdecimal-impl

uuid-impl

bson-uuid-impl

bytes-impl

url-impl

indexmap-impl

ordered-float-impl

heapless-impl

semver-impl

Clone this wiki locally