macro_rules! branded {
(
$(#[$attr:meta])*
$vis:vis struct $ident:ident<$($generic:ident),+ $(,)?> ( $ty:ty ) $(as $ts_name:literal)?
) => { ... };
(
$(#[$attr:meta])*
$vis:vis struct $ident:ident ( $ty:ty ) $(as $ts_name:literal)?
) => { ... };
(@brand $ident:ident $ts_name:literal) => { ... };
(@brand $ident:ident) => { ... };
}Expand description
Create a branded tuple struct type that exports to TypeScript with a custom name.
This macro generates a single-field tuple struct and implements the Type trait
for it, allowing you to create “branded” types that maintain distinct identities
in TypeScript.
§Examples
Basic usage:
ⓘ
branded!(pub struct AccountId(String));With custom TypeScript name:
ⓘ
branded!(pub struct AccountId(String) as "accountId");With attributes:
ⓘ
branded!(#[derive(Serialize)] pub struct UserId(String));With generics:
ⓘ
branded!(pub struct Id<T>(T) as "id");§Requirements
This macro requires that the specta crate is in scope and available as a dependency.
§Notes
- The struct must be a tuple struct with exactly one field
- The
Typeimplementation is currently atodo!()placeholder - The
as "name"syntax is optional; if omitted, the struct name is used