ts-type
ts-type helps Rust code reason about TypeScript types. It provides a
structured representation of TypeScript syntax together with helper macros and
conversions that are friendly to procedural macros such as
ts-macro.
Highlights
TsTypeenum models common TypeScript constructs (unions, tuples, generics, indexed access, arrays) with predictableDisplayoutput.ts_type!macro buildsTsTypevalues using TypeScript syntax and allows interpolation of previously built fragments.- Parser and
ToTsTypetrait turn&strorsyn::Typevalues intoTsType, with special behaviour forOption<T>andVec<T>to map toT | undefinedandT[]respectively. - Lightweight
TsTypeErrorandtype_error_at!macro surface precise parsing locations when something cannot be represented.
Usage
Compose a type inline
use ;
let binding: TsType = ts_type!;
assert_eq!;
Convert a syn::Type
use parse_quote;
use ToTsType;
let rust_ty: Type = parse_quote!;
let ts_ty = rust_ty.to_ts_type.unwrap;
assert_eq!;
Pair with ts-macro
ts-type powers the #[ts] attribute. The macro uses
ToTsType to infer field types and compose the final TypeScript interface that
gets emitted alongside your bindings.
License
Licensed under the Apache-2.0 license.