Derive Macro typescript_type_def::TypeDef[][src]

#[derive(TypeDef)]
{
    // Attributes available to this derive:
    #[type_def]
    #[serde]
}
Expand description

A derive proc-macro for the TypeDef trait.

This macro can be used on structs and enums which also derive serde::Serialize and/or serde::Deserialize, and will generate a TypeDef implementation which matches the shape of the JSON produced by using serde_json on the target type. This macro will also read and adapt to #[serde(...)] attributes on the target type’s definition.

This macro also reads the following attributes:

  • #[type_def(namespace = "x.y.z")] on the struct/enum body puts the TypeScript type definition under a namespace of x.y.z. Note that write_definition_file will additionally place all type definitions under a root namespace.
  • #[type_def(type_of = "T")] on a struct or tuple field will use the Rust type T’s TypeScript definition instead of the field’s type. T must be a valid Rust type which implements TypeDef and whose JSON format matches the JSON format of the field’s type. This attribute can be used to specify the type definition for a foreign type using your own type.

serde attribute support

Legend:

  • ✓ - full support
  • ? - may support in the future
  • ✗ - will not support

Container Attributes

AttributeSupport
#[serde(rename = "name")]
#[serde(rename_all = "...")]
#[serde(deny_unknown_fields)]?
#[serde(tag = "type")]
#[serde(tag = "t", content = "c")]
#[serde(untagged)]
#[serde(bound = "T: MyTrait")]?
#[serde(default)]?
#[serde(default = "path")]?
#[serde(remote = "...")]
#[serde(transparent)]
#[serde(from = "FromType")]
#[serde(try_from = "FromType")]
#[serde(into = "IntoType")]
#[serde(crate = "...")]

Variant Attributes

AttributeSupport
#[serde(rename = "name")]
#[serde(alias = "name")]?
#[serde(rename_all = "...")]
#[serde(skip)]
#[serde(skip_serializing)]
#[serde(skip_deserializing)]
#[serde(serialize_with = "path")]
#[serde(deserialize_with = "path")]
#[serde(with = "module")]
#[serde(bound = "T: MyTrait")]?
#[serde(borrow)]?
#[serde(borrow = "'a + 'b + ...")]?
#[serde(other)]?

Field Attributes

AttributeSupport
#[serde(rename = "name")]
#[serde(alias = "name")]?
#[serde(default)]
#[serde(default = "path")]
#[serde(flatten)]
#[serde(skip)]
#[serde(skip_serializing)]
#[serde(skip_deserializing)]
#[serde(skip_serializing_if = "path")]
#[serde(serialize_with = "path")]
#[serde(deserialize_with = "path")]
#[serde(with = "module")]
#[serde(borrow)]?
#[serde(borrow = "'a + 'b + ...")]?
#[serde(bound = "T: MyTrait")]?
#[serde(getter = "...")]