Skip to main content

TypescriptUnion

Derive Macro TypescriptUnion 

Source
#[derive(TypescriptUnion)]
{
    // Attributes available to this derive:
    #[ts]
}
Expand description

为 Rust 枚举生成 TypeScript 联合类型

§示例

use typescript_macros::TypescriptUnion;

#[derive(TypescriptUnion)]
enum Shape {
    Circle { radius: f64 },
    Rectangle { width: f64, height: f64 },
    Triangle { base: f64, height: f64 },
}

这将生成对应的 TypeScript 联合类型:

type Shape =
    | { type: "Circle"; radius: number }
    | { type: "Rectangle"; width: number; height: number }
    | { type: "Triangle"; base: number; height: number };