Trait ts_rs::TS [−][src]
pub trait TS: 'static {
const EXPORT_TO: Option<&'static str>;
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> { ... }
}
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 tobindings/<name>.ts
when runningcargo test
-
#[ts(export_to = "..")]
:
Specifies where the type should be exported to. Defaults tobindings/<name>.ts
. -
#[ts(rename = "..")]
:
Sets the typescript name of the generated type -
#[ts(rename_all = "..")]
:
Rename all fields/variants of the type. Valid values arelowercase
,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 deriveTS
. -
#[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 arelowercase
,UPPERCASE
,camelCase
,snake_case
,PascalCase
,SCREAMING_SNAKE_CASE
enum variant attributes
-
#[ts(rename = "..")]
:
Renames this variant -
#[ts(skip)]
:
Skip this variant
Associated Constants
Required methods
fn dependencies() -> Vec<Dependency>
fn dependencies() -> Vec<Dependency>
Information about types this type depends on. This is used for resolving imports when exporting to a file.
fn transparent() -> bool
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
Declaration of this type, e.g. interface User { user_id: number, ... }
.
This function will panic if the type has no declaration.
fn name_with_type_args(args: Vec<String>) -> String
fn name_with_type_args(args: Vec<String>) -> String
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.
fn inline_flattened() -> String
fn inline_flattened() -> String
Flatten an type declaration.
This function will panic if the type cannot be flattened.
fn export() -> Result<(), ExportError>
fn export() -> Result<(), ExportError>
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.