22个版本

0.5.11 2024年2月9日
0.5.9 2023年11月28日
0.5.7 2023年7月9日
0.5.6 2023年2月14日
0.2.1 2021年7月10日

#27 in #generate-typescript

Download history 459/week @ 2024-03-14 319/week @ 2024-03-21 1386/week @ 2024-03-28 822/week @ 2024-04-04 456/week @ 2024-04-11 624/week @ 2024-04-18 920/week @ 2024-04-25 815/week @ 2024-05-02 1144/week @ 2024-05-09 1215/week @ 2024-05-16 530/week @ 2024-05-23 840/week @ 2024-05-30 341/week @ 2024-06-06 530/week @ 2024-06-13 291/week @ 2024-06-20 416/week @ 2024-06-27

1,853 下载/每月
8 个crate中使用 (via typescript-type-def)

MIT 许可证

37KB
1K SLoC

Crates.io Build Status docs.rs

typescript-type-def

为Rust类型生成TypeScript类型定义。

此crate允许您生成一个包含类型定义的TypeScript模块,这些类型定义描述了Rust类型的JSON序列化。预期用途是为使用 serde_json 从Rust类型序列化为JSON的数据定义TypeScript类型,这样就可以在TypeScript中使用而不需要维护一组并行的类型定义。

此crate的一个用途示例是在一个带有Rust后端和TypeScript前端的项目中工作。如果用于在两个之间通信的数据在Rust中定义并使用 serde_json 对其进行编码/解码以在网络中传输,则可以使用此crate自动生成这些类型的TypeScript定义文件,以便在您的前端代码中安全使用。如果使用此crate作为服务器构建脚本的一部分将定义文件写入TypeScript源代码目录,则此过程甚至可以完全自动化。

您还可以使用由本库生成的 Typescript 类型信息来编写使用这些类型定义的 Typescript 代码。实现了 TypeDef 的 Rust 类型具有关联的常量 TypeDef::INFO,该常量有一个用于此目的的方法 TypeInfo::write_ref_expr

功能

  • json_value - 为 serde_json 中的 JSON 值类型添加 TypeDef 实现。

示例

简单示例

use serde::Serialize;
use typescript_type_def::{
    write_definition_file,
    DefinitionFileOptions,
    TypeDef,
};

#[derive(Serialize, TypeDef)]
struct Foo {
    a: usize,
    b: String,
}

let ts_module = {
    let mut buf = Vec::new();
    let options = DefinitionFileOptions::default();
    write_definition_file::<_, Foo>(&mut buf, options).unwrap();
    String::from_utf8(buf).unwrap()
};
assert_eq!(
    ts_module,
    r#"// AUTO-GENERATED by typescript-type-def

export default types;
export namespace types{
export type Usize=number;
export type Foo={"a":types.Usize;"b":string;};
}
"#
);

let foo = Foo {
    a: 123,
    b: "hello".to_owned(),
};
let json = serde_json::to_string(&foo).unwrap();
// This JSON matches the TypeScript type definition above
assert_eq!(json, r#"{"a":123,"b":"hello"}"#);

当处理由许多类型组成的大型代码库时,一个有用的模式是声明一个 "API" 类型别名,该别名列出您希望为其创建定义的所有类型。例如

use serde::Serialize;
use typescript_type_def::{write_definition_file, TypeDef};

#[derive(Serialize, TypeDef)]
struct Foo {
    a: String,
}

#[derive(Serialize, TypeDef)]
struct Bar {
    a: String,
}

#[derive(Serialize, TypeDef)]
struct Baz {
    a: Qux,
}

#[derive(Serialize, TypeDef)]
struct Qux {
    a: String,
}

// This type lists all the top-level types we want to make definitions for.
// You don't need to list *every* type in your API here, only ones that
// wouldn't be referenced otherwise. Note that `Qux` is not mentioned, but
// is still emitted because it is a dependency of `Baz`.
type Api = (Foo, Bar, Baz);

let ts_module = {
    let mut buf = Vec::new();
    write_definition_file::<_, Api>(&mut buf, Default::default()).unwrap();
    String::from_utf8(buf).unwrap()
};
assert_eq!(
    ts_module,
    r#"// AUTO-GENERATED by typescript-type-def

export default types;
export namespace types{
export type Foo={"a":string;};
export type Bar={"a":string;};
export type Qux={"a":string;};
export type Baz={"a":types.Qux;};
}
"#
);

或者,您可以使用 write_definition_file_from_type_infos 与在运行时创建的 TypeInfo 引用列表来创建定义文件。例如

use serde::Serialize;
use typescript_type_def::{write_definition_file_from_type_infos, TypeDef};

#[derive(Serialize, TypeDef)]
struct Foo {
    a: String,
}

#[derive(Serialize, TypeDef)]
struct Bar {
    a: String,
}

#[derive(Serialize, TypeDef)]
struct Baz {
    a: Qux,
}

#[derive(Serialize, TypeDef)]
struct Qux {
    a: String,
}

// This list contains type info for all the top-level types we want to make
// definitions for.
// You don't need to list *every* type in your API here, only ones that
// wouldn't be referenced otherwise. Note that `Qux` is not mentioned, but
// is still emitted because it is a dependency of `Baz`.
let api = vec![
    &Foo::INFO,
    &Bar::INFO,
    &Baz::INFO,
];

let ts_module = {
    let mut buf = Vec::new();
    write_definition_file_from_type_infos(
        &mut buf,
        Default::default(),
        &api,
    )
    .unwrap();
    String::from_utf8(buf).unwrap()
};
assert_eq!(
    ts_module,
    r#"// AUTO-GENERATED by typescript-type-def

export default types;
export namespace types{
export type Foo={"a":string;};
export type Bar={"a":string;};
export type Qux={"a":string;};
export type Baz={"a":types.Qux;};
}
"#
);

lib.rs:

此 crate 定义了一个过程宏 derive 宏,用于从 typescript_type_def crate 实现 TypeDef 特性。

有关更多信息,请参阅该 crate 的文档。

依赖项

~2MB
~43K SLoC