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

These types will be converted to a TypeScript string

  • NaiveDateTime
  • NaiveDate
  • NaiveTime
  • Month
  • Weekday
  • Duration
  • DateTime<T: chrono::TimeZone + ts_rs::TS + 'static>
  • Date<T: chrono::TimeZone + ts_rs::TS + 'static>

These types don't actually generate any TypeScript code, but they need to implement TS to support Date and DateTime

  • Utc
  • Local
  • FixedOffset

bigdecimal-impl

bigdecimal::BigDecimal will be converted into string

uuid-impl

uuid::Uuid will be converted into string

bson-uuid-impl

bson::Uuid will be converted into string

bytes-impl

bytes::Bytes and bytes::BytesMut will be converted into Array<number>

url-impl

url::Url will be converted into string

indexmap-impl

indexmap::IndexSet<T: TS> will be converted into Array<T> indexmap::IndexMap<K: TS, V: TS> will be converted into Record<K, V>

ordered-float-impl

ordered_float::OrderedFloat<f32> and ordered_float::OrderedFloat<f64> will be converted into number

heapless-impl

heapless::Vec<T: TS, const N: usize> will be converted into Array<T>

semver-impl

semver::Version will be converted into string