Trait ts_rs::TS [−][src]
pub trait TS: 'static {
fn name() -> String;
fn dependencies() -> Vec<(TypeId, String)>;
fn transparent() -> bool;
fn decl() -> String { ... }
fn inline(indent: usize) -> String { ... }
fn inline_flattened(indent: usize) -> String { ... }
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 arelowercase,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 -
#[ts(skip)]:
Skip this field -
#[ts(flatten)]:
Flatten this field (only works if the field is a struct)
enum attributes
-
#[ts(rename = "..")]:
Set the name of the generated type -
#[ts(rename_all = "..")]:
Rename all variants of this enum.
Valid values arelowercase,UPPERCASE,camelCase,snake_case,PascalCase,SCREAMING_SNAKE_CASE
enum variant attributes
-
#[ts(rename = "..")]:
Renames this variant -
#[ts(skip)]:
Skip this variant
Required methods
fn name() -> String[src]
Name of this type in TypeScript.
fn dependencies() -> Vec<(TypeId, String)>[src]
All type ids and typescript names of the types this type depends on.
This is used for resolving imports when using the export! macro.
fn transparent() -> bool[src]
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
fn decl() -> String[src]
Declaration of this type, e.g. interface User { user_id: number, ... }.
This function will panic if the type has no declaration.
fn inline(indent: usize) -> String[src]
Formats this types definition in TypeScript, e.g { user_id: number }.
This function will panic if the type cannot be inlined.
fn inline_flattened(indent: usize) -> String[src]
Flatten an type declaration.
This function will panic if the type cannot be flattened.
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.
Implementations on Foreign Types
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<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<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<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<T4: TS, T5: TS, T6: TS, T7: TS, T8: TS, T9: TS, T10: TS> TS for (T4, T5, T6, T7, T8, T9, T10)[src]