Skip to main content

oak_typescript/ast/
type_nodes.rs

1use crate::ast::{ClassMember, FunctionParam, LiteralType};
2use core::range::Range;
3
4/// Represents a TypeScript type annotation.
5#[derive(Debug, Clone)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub enum TypeAnnotation {
8    /// An identifier type.
9    Identifier(String),
10    /// A predefined type (e.g., `any`, `number`, `string`, `boolean`, `void`, `never`).
11    Predefined(String),
12    /// A literal type.
13    Literal(LiteralType),
14    /// An array type.
15    Array(Box<TypeAnnotation>),
16    /// A tuple type.
17    Tuple(Vec<TypeAnnotation>),
18    /// A union type.
19    Union(Vec<TypeAnnotation>),
20    /// An intersection type.
21    Intersection(Vec<TypeAnnotation>),
22    /// A type reference with optional type arguments.
23    Reference {
24        /// Name of the referenced type.
25        name: String,
26        /// Type arguments.
27        args: Vec<TypeAnnotation>,
28    },
29    /// A function type.
30    Function {
31        /// Type parameters of the function type.
32        type_params: Vec<TypeParameter>,
33        /// Parameters of the function type.
34        args: Vec<FunctionParam>,
35        /// Return type of the function type.
36        return_type: Box<TypeAnnotation>,
37    },
38    /// An object type (interface-like).
39    Object(Vec<ClassMember>),
40    /// A type query (`typeof X`).
41    Query(String),
42    /// A `keyof` type.
43    KeyOf(Box<TypeAnnotation>),
44    /// A conditional type (`T extends U ? X : Y`).
45    Conditional {
46        /// Type being checked.
47        check_type: Box<TypeAnnotation>,
48        /// Type to extend.
49        extends_type: Box<TypeAnnotation>,
50        /// Type if the condition is true.
51        true_type: Box<TypeAnnotation>,
52        /// Type if the condition is false.
53        false_type: Box<TypeAnnotation>,
54    },
55    /// A mapped type (`{ [K in T]: U }`).
56    Mapped {
57        /// Name of the key variable.
58        key_name: String,
59        /// Type of the key variable.
60        key_type: Box<TypeAnnotation>,
61        /// Type of the value.
62        value_type: Box<TypeAnnotation>,
63        /// Readonly modifier.
64        readonly: Option<bool>,
65        /// Optional modifier.
66        optional: Option<bool>,
67    },
68    /// A template literal type.
69    TemplateLiteral(Vec<TemplateElement>),
70    /// An `infer` type.
71    Infer(String),
72}
73
74/// Represents a type parameter in a generic declaration.
75#[derive(Debug, Clone)]
76#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
77pub struct TypeParameter {
78    /// Name of the type parameter.
79    pub name: String,
80    /// Constraint on the type parameter (`extends T`).
81    pub constraint: Option<TypeAnnotation>,
82    /// Default type of the type parameter (`= T`).
83    pub default: Option<TypeAnnotation>,
84    /// Source span of the type parameter.
85    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
86    pub span: Range<usize>,
87}
88
89/// Represents an element in a template literal type.
90#[derive(Debug, Clone)]
91#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
92pub enum TemplateElement {
93    /// A literal string element.
94    String(String),
95    /// A type element.
96    Type(Box<TypeAnnotation>),
97}