[−][src]Trait ts_rs::TS
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
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
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)
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.