Trait ts_rs::TS

source · []
pub trait TS: 'static {
    const EXPORT_TO: Option<&'static str> = None;

    fn name() -> String;
    fn dependencies() -> Vec<Dependency>;
    fn transparent() -> bool;

    fn decl() -> String { ... }
    fn name_with_type_args(args: Vec<String>) -> String { ... }
    fn inline() -> String { ... }
    fn inline_flattened() -> String { ... }
    fn export() -> Result<(), ExportError> { ... }
    fn export_to(path: impl AsRef<Path>) -> Result<(), ExportError> { ... }
    fn export_to_string() -> Result<String, ExportError> { ... }
}
Expand description

A type which can be represented in TypeScript.
Most of the time, you’d want to derive this trait instead of implementing it manually.
ts-rs comes with implementations for all primitives, most collections, tuples, arrays and containers.

exporting

Because Rusts procedural macros are evaluated before other compilation steps, TypeScript bindings cannot be exported during compile time. Bindings can be exported within a test, which ts-rs generates for you by adding #[ts(export)] to a type you wish to export to a file. If, for some reason, you need to do this during runtime, you can call TS::export yourself.

serde compatibility

By default, the feature serde-compat is enabled. ts-rs then parses serde attributes and adjusts the generated typescript bindings accordingly. Not all serde attributes are supported yet - if you use an unsupported attribute, you’ll see a warning.

container attributes

attributes applicable for both structs and enums

  • #[ts(export)]:
    Generates a test which will export the type, by default to bindings/<name>.ts when running cargo test

  • #[ts(export_to = "..")]:
    Specifies where the type should be exported to. Defaults to bindings/<name>.ts.
    If the provided path ends in a trailing /, it is interpreted as a directory.

  • #[ts(rename = "..")]:
    Sets the typescript name of the generated type

  • #[ts(rename_all = "..")]:
    Rename all fields/variants of the type. Valid values are lowercase, UPPERCASE, camelCase, snake_case, PascalCase, SCREAMING_SNAKE_CASE

struct field attributes

  • #[ts(type = "..")]:
    Overrides the type used in TypeScript.
    This is useful when there’s a type for which you cannot derive TS.

  • #[ts(rename = "..")]:
    Renames this field

  • #[ts(inline)]:
    Inlines the type of this field

  • #[ts(skip)]:
    Skip this field

  • #[ts(optional)]:
    Indicates the field may be omitted from the serialized struct

  • #[ts(flatten)]:
    Flatten this field (only works if the field is a struct)

enum attributes

  • #[ts(tag = "..")]:
    Changes the representation of the enum to store its tag in a separate field. See the serde docs.

  • #[ts(content = "..")]:
    Changes the representation of the enum to store its content in a separate field. See the serde docs.

  • #[ts(untagged)]:
    Changes the representation of the enum to not include its tag. See the serde docs.

  • #[ts(rename_all = "..")]:
    Rename all variants of this enum.
    Valid values are lowercase, UPPERCASE, camelCase, snake_case, PascalCase, SCREAMING_SNAKE_CASE

enum variant attributes

  • #[ts(rename = "..")]:
    Renames this variant

  • #[ts(skip)]:
    Skip this variant

Provided Associated Constants

Required Methods

Name of this type in TypeScript.

Information about types this type depends on. This is used for resolving imports when exporting to a file.

true if this is a transparent type, e.g tuples or a list.
This is used for resolving imports when using the export! macro.

Provided Methods

Declaration of this type, e.g. interface User { user_id: number, ... }. This function will panic if the type has no declaration.

Name of this type in TypeScript, with type arguments.

Formats this types definition in TypeScript, e.g { user_id: number }. This function will panic if the type cannot be inlined.

Flatten an type declaration.
This function will panic if the type cannot be flattened.

Manually export this type to a file. The output file can be specified by annotating the type with #[ts(export_to = ".."]. By default, the filename will be derived from the types name.

When a type is annotated with #[ts(export)], it is exported automatically within a test. This function is only usefull if you need to export the type outside of the context of a test.

Manually export this type to a file with a file with the specified path. This function will ignore the #[ts(export_to = "..)] attribute.

Manually generate bindings for this type, returning a String.
This function does not format the output, even if the format feature is enabled.

Implementations on Foreign Types

Implementors