3 stable releases
Uses new Rust 2024
| 1.0.2 | Jan 8, 2026 |
|---|---|
| 1.0.1 | Apr 17, 2025 |
#159 in FFI
Used in ts-macro
43KB
868 lines
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 ts_type::{ts_type, TsType};
let binding: TsType = ts_type!(Record<string, (number | boolean)[]>);
assert_eq!(binding.to_string(), "Record<string, (number | boolean)[]>");
Convert a syn::Type
use syn::parse_quote;
use ts_type::ToTsType;
let rust_ty: syn::Type = parse_quote!(Option<Vec<String>>);
let ts_ty = rust_ty.to_ts_type().unwrap();
assert_eq!(ts_ty.to_string(), "string[] | undefined");
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.
Dependencies
~3.5–4.5MB
~90K SLoC