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 tobindings/<name>.ts
when runningcargo test
-
#[ts(export_to = "..")]
:
Specifies where the type should be exported to. Defaults tobindings/<name>.ts
.
If the provided path ends in a trailing/
, it is interpreted as a directory.
Note that you need to add theexport
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 arelowercase
,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 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
, “kebab-case”
§enum variant attributes
-
#[ts(rename = "..")]
:
Renames this variant -
#[ts(skip)]
:
Skip this variant
Provided Associated Constants§
Required Methods§
sourcefn dependencies() -> Vec<Dependency>where
Self: 'static,
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.
sourcefn 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§
sourcefn decl() -> String
fn decl() -> String
Declaration of this type, e.g. interface User { user_id: number, ... }
.
This function will panic if the type has no declaration.
sourcefn 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.
sourcefn inline() -> String
fn inline() -> String
Formats this types definition in TypeScript, e.g { user_id: number }
.
This function will panic if the type cannot be inlined.
sourcefn inline_flattened() -> String
fn inline_flattened() -> String
Flatten an type declaration.
This function will panic if the type cannot be flattened.
sourcefn export() -> Result<(), ExportError>where
Self: 'static,
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.
sourcefn export_to(path: impl AsRef<Path>) -> Result<(), ExportError>where
Self: 'static,
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.
sourcefn export_to_string() -> Result<String, ExportError>where
Self: 'static,
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.