Skip to main content

TypescriptGuard

Derive Macro TypescriptGuard 

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

为 Rust 类型生成 TypeScript 类型守卫

§示例

use typescript_macros::TypescriptGuard;

#[derive(TypescriptGuard)]
struct User {
    id: u32,
    name: String,
    active: bool,
}

这将生成对应的 TypeScript 类型守卫:

function isUser(obj: any): obj is User {
    return (
        typeof obj === 'object' && obj !== null &&
        typeof obj.id === 'number' &&
        typeof obj.name === 'string' &&
        typeof obj.active === 'boolean'
    );
}