-
Notifications
You must be signed in to change notification settings - Fork 116
Feature flags
ts-rs has feature flags to help you with serde compatibility,
formatting the output TS files, using ES Modules style import
s
and implementing TS
for types of some popular libraries.
These features change the behavior of ts-rs
in certain ways
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.
Formats the resulting TS code using dprint-plugin-typescript
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
The ES Modules spec requires import
statements to include the imported
file's extension. This flag will append .js
to generated import paths
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.
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::BigDecimal
will be converted into string
uuid::Uuid
will be converted into string
bson::Uuid
will be converted into string
bytes::Bytes
and bytes::BytesMut
will be converted into
Array<number>
url::Url
will be converted into string
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::OrderedFloat<f32>
and ordered_float::OrderedFloat<f64>
will be converted into number
heapless::Vec<T: TS, const N: usize>
will be converted into Array<T>
semver::Version
will be converted into string