[][src]Trait ts_rs::TS

pub trait TS {
    pub fn format(indent: usize, inline: bool) -> String;

    pub fn decl() -> Option<String> { ... }
pub fn dump(out: impl AsRef<Path>) -> Result<()> { ... } }

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 numeric types, String, Vec, Option and tuples.

get started

TS can easily be derived for structs and enums:

use ts_rs::TS;

#[derive(TS)]
struct User {
    first_name: String,
    last_name: String,
}

To actually obtain the bindings, you can call User::dump to write the bindings to a file:


std::fs::remove_file("bindings.ts").ok();
User::dump("bindings.ts").unwrap();

struct attributes

  • #[ts(rename = "..")]:
    Set the name of the generated interface

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

struct field attributes

  • #[ts(type = "..")]:
    Overrides the type used in TypeScript

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

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

enum attributes

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

  • #[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

Required methods

pub fn format(indent: usize, inline: bool) -> String[src]

Formats this type. When using inline, this will return the definition of the type. Otherwise, it's name is returned (if the type is named)

Loading content...

Provided methods

pub fn decl() -> Option<String>[src]

Declaration of this type, e.g. interface User { user_id: number, ... }, if available.

pub fn dump(out: impl AsRef<Path>) -> Result<()>[src]

Dumps the declaration of this type to a file.
If the file does not exist, it will be created.
If it does, the declaration will be appended.

This function will panicked when called on a type which does not have a declaration.

Loading content...

Implementations on Foreign Types

impl TS for u8[src]

impl TS for i8[src]

impl TS for u16[src]

impl TS for i16[src]

impl TS for u32[src]

impl TS for i32[src]

impl TS for u64[src]

impl TS for i64[src]

impl TS for f32[src]

impl TS for f64[src]

impl TS for u128[src]

impl TS for i128[src]

impl TS for bool[src]

impl TS for String[src]

impl<'_> TS for &'_ str[src]

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)[src]

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)[src]

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)[src]

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

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

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

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

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

impl<T9: TS, T10: TS> TS for (T9, T10)[src]

impl<T10: TS> TS for (T10,)[src]

impl<T: TS, '_> TS for &'_ T[src]

impl<T: TS> TS for Box<T>[src]

impl<T: TS> TS for Arc<T>[src]

impl<T: TS> TS for Rc<T>[src]

impl<'a, T: TS + ToOwned> TS for Cow<'a, T>[src]

impl<T: TS> TS for Cell<T>[src]

impl<T: TS> TS for RefCell<T>[src]

impl<T: TS> TS for Option<T>[src]

impl<T: TS> TS for Vec<T>[src]

Loading content...

Implementors

Loading content...