Skip to main content

oak_typescript/ast/
type_nodes.rs

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