Trait ts_rs::TS

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

    // Required methods
    fn name() -> String;
    fn dependencies() -> Vec<Dependency>
       where Self: 'static;
    fn transparent() -> bool;

    // Provided methods
    fn decl() -> String { ... }
    fn name_with_type_args(args: Vec<String>) -> String { ... }
    fn inline() -> String { ... }
    fn inline_flattened() -> String { ... }
    fn export() -> Result<(), ExportError>
       where Self: 'static { ... }
    fn export_to(path: impl AsRef<Path>) -> Result<(), ExportError>
       where Self: 'static { ... }
    fn export_to_string() -> Result<String, ExportError>
       where Self: 'static { ... }
}
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.
    Note that you need to add the export attribute as well, in order to generate a test which exports the type.

  • #[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, “kebab-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, “kebab-case”

§enum variant attributes

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

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

Provided Associated Constants§

source

const EXPORT_TO: Option<&'static str> = None

Required Methods§

source

fn name() -> String

Name of this type in TypeScript.

source

fn dependencies() -> Vec<Dependency>
where Self: 'static,

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

source

fn transparent() -> bool

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§

source

fn decl() -> String

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

source

fn name_with_type_args(args: Vec<String>) -> String

Name of this type in TypeScript, with type arguments.

source

fn inline() -> String

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

source

fn inline_flattened() -> String

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

source

fn export() -> Result<(), ExportError>
where Self: 'static,

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.

source

fn export_to(path: impl AsRef<Path>) -> Result<(), ExportError>
where Self: 'static,

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

source

fn export_to_string() -> Result<String, ExportError>
where Self: 'static,

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

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl TS for IpAddr

source§

impl TS for SocketAddr

source§

impl TS for bool

source§

impl TS for char

source§

impl TS for f32

source§

impl TS for f64

source§

impl TS for i8

source§

impl TS for i16

source§

impl TS for i32

source§

impl TS for i64

source§

impl TS for i128

source§

impl TS for isize

source§

impl TS for str

source§

impl TS for u8

source§

impl TS for u16

source§

impl TS for u32

source§

impl TS for u64

source§

impl TS for u128

source§

impl TS for ()

source§

impl TS for usize

source§

impl TS for String

source§

impl TS for Ipv4Addr

source§

impl TS for Ipv6Addr

source§

impl TS for SocketAddrV4

source§

impl TS for SocketAddrV6

source§

impl TS for Path

source§

impl TS for PathBuf

source§

impl TS for NonZeroI8

source§

impl TS for NonZeroI16

source§

impl TS for NonZeroI32

source§

impl TS for NonZeroI64

source§

impl TS for NonZeroI128

source§

impl TS for NonZeroIsize

source§

impl TS for NonZeroU8

source§

impl TS for NonZeroU16

source§

impl TS for NonZeroU32

source§

impl TS for NonZeroU64

source§

impl TS for NonZeroU128

source§

impl TS for NonZeroUsize

source§

impl<'a, T: TS + ToOwned + ?Sized> TS for Cow<'a, T>

source§

impl<'a, T: TS + ?Sized> TS for &T

source§

impl<I: TS> TS for Range<I>

source§

impl<I: TS> TS for RangeInclusive<I>

source§

impl<K: TS, V: TS> TS for BTreeMap<K, V>

source§

impl<K: TS, V: TS> TS for HashMap<K, V>

source§

impl<T1: TS, T2: TS, T3: TS, T4: TS, T5: TS, T6: TS, T7: TS, T8: TS, T9: TS, T10: TS> TS for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)

source§

impl<T2: TS, T3: TS, T4: TS, T5: TS, T6: TS, T7: TS, T8: TS, T9: TS, T10: TS> TS for (T2, T3, T4, T5, T6, T7, T8, T9, T10)

source§

impl<T3: TS, T4: TS, T5: TS, T6: TS, T7: TS, T8: TS, T9: TS, T10: TS> TS for (T3, T4, T5, T6, T7, T8, T9, T10)

source§

impl<T4: TS, T5: TS, T6: TS, T7: TS, T8: TS, T9: TS, T10: TS> TS for (T4, T5, T6, T7, T8, T9, T10)

source§

impl<T5: TS, T6: TS, T7: TS, T8: TS, T9: TS, T10: TS> TS for (T5, T6, T7, T8, T9, T10)

source§

impl<T6: TS, T7: TS, T8: TS, T9: TS, T10: TS> TS for (T6, T7, T8, T9, T10)

source§

impl<T7: TS, T8: TS, T9: TS, T10: TS> TS for (T7, T8, T9, T10)

source§

impl<T8: TS, T9: TS, T10: TS> TS for (T8, T9, T10)

source§

impl<T9: TS, T10: TS> TS for (T9, T10)

source§

impl<T10: TS> TS for (T10,)

source§

impl<T: TS + ?Sized> TS for Box<T>

source§

impl<T: TS + ?Sized> TS for Rc<T>

source§

impl<T: TS + ?Sized> TS for Arc<T>

source§

impl<T: TS + ?Sized> TS for Weak<T>

source§

impl<T: TS> TS for Option<T>

source§

impl<T: TS> TS for [T]

source§

impl<T: TS> TS for BTreeSet<T>

source§

impl<T: TS> TS for Vec<T>

source§

impl<T: TS> TS for Cell<T>

source§

impl<T: TS> TS for RefCell<T>

source§

impl<T: TS> TS for PhantomData<T>

source§

impl<T: TS> TS for HashSet<T>

source§

impl<T: TS> TS for Mutex<T>

source§

impl<T: TS, E: TS> TS for Result<T, E>

source§

impl<T: TS, const N: usize> TS for [T; N]

Implementors§