pub enum TypeDef {
Show 13 variants
Primitive(Primitive),
Array(Box<TypeDef>),
Tuple(Vec<TypeDef>),
Object(Vec<Field>),
Union(Vec<TypeDef>),
Intersection(Vec<TypeDef>),
Record {
key: Box<TypeDef>,
value: Box<TypeDef>,
},
Named {
namespace: Vec<String>,
name: String,
def: Box<TypeDef>,
module: Option<String>,
},
Ref(String),
Literal(Literal),
Function {
params: Vec<Field>,
return_type: Box<TypeDef>,
},
Generic {
base: String,
args: Vec<TypeDef>,
},
TemplateLiteral {
strings: Vec<String>,
types: Vec<Box<TypeDef>>,
},
}Expand description
Intermediate representation for TypeScript types.
This enum represents all TypeScript types that ferrotype can generate. It serves as the IR between Rust types and TypeScript output, enabling analysis and transformation before rendering.
§Type Categories
- Primitives:
string,number,boolean,null, etc. - Compounds: Arrays, tuples, objects, unions, intersections
- References: Named types and type references
- Literals: Specific string, number, or boolean values
Variants§
Primitive(Primitive)
A primitive TypeScript type.
Array(Box<TypeDef>)
An array type: T[]
Tuple(Vec<TypeDef>)
A tuple type: [T1, T2, ...]
Object(Vec<Field>)
An object type with named fields: { field1: T1; field2?: T2; }
Union(Vec<TypeDef>)
A union type: T1 | T2 | ...
Intersection(Vec<TypeDef>)
An intersection type: T1 & T2 & ...
Record
A record/dictionary type: Record<K, V> or { [key: K]: V }
Named
A named type definition that should be emitted as a separate declaration. This is the primary mechanism for type deduplication.
The optional namespace field allows placing types in TypeScript namespaces:
namespace: vec![]→type State = ...namespace: vec!["VM".into(), "Git".into()]→namespace VM { namespace Git { type State = ... } }
Fields
Ref(String)
A reference to a named type. Used to avoid infinite recursion and to generate cleaner output by referencing previously-defined types.
Literal(Literal)
A literal type with a specific value.
Function
A function type: (arg1: T1, arg2: T2) => R
Generic
A generic type application: Generic<T1, T2>
TemplateLiteral
A template literal type: `prefix${Type}suffix`
Template literal types enable compile-time string pattern validation in TypeScript.
They’re commonly used for branded IDs like vm-${string} or version patterns.
§Example
// `vm-${string}` validates that strings start with "vm-"
let vm_id = TypeDef::TemplateLiteral {
strings: vec!["vm-".into(), "".into()],
types: vec![Box::new(TypeDef::Primitive(Primitive::String))],
};
assert_eq!(vm_id.render(), "`vm-${string}`");