tsz-solver 0.1.8

TypeScript type solver for the tsz compiler
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! Additional type query classifiers.
//!
//! Contains classification enums and functions for specific checker scenarios:
//! - Excess property checking
//! - Constructor access levels
//! - Assignability evaluation
//! - Binding element type extraction
//! - Type identity/accessor helpers
//! - Symbol resolution traversal
//! - Interface merge type classification
//! - Augmentation target classification

use crate::{TypeData, TypeDatabase, TypeId};

// =============================================================================
// Excess Properties Classification
// =============================================================================

/// Classification for checking excess properties.
#[derive(Debug, Clone)]
pub enum ExcessPropertiesKind {
    /// Object type (without index signature) - check for excess
    Object(crate::types::ObjectShapeId),
    /// Object with index signature - accepts any property
    ObjectWithIndex(crate::types::ObjectShapeId),
    /// Union - check all members
    Union(Vec<TypeId>),
    /// Intersection - merge known members from all object constituents
    Intersection(Vec<TypeId>),
    /// Not an object type
    NotObject,
}

/// Classify a type for excess property checking.
pub fn classify_for_excess_properties(
    db: &dyn TypeDatabase,
    type_id: TypeId,
) -> ExcessPropertiesKind {
    let Some(key) = db.lookup(type_id) else {
        return ExcessPropertiesKind::NotObject;
    };

    match key {
        TypeData::Object(shape_id) => ExcessPropertiesKind::Object(shape_id),
        TypeData::ObjectWithIndex(shape_id) => ExcessPropertiesKind::ObjectWithIndex(shape_id),
        TypeData::Union(list_id) => {
            let members = db.type_list(list_id);
            ExcessPropertiesKind::Union(members.to_vec())
        }
        TypeData::Intersection(list_id) => {
            let members = db.type_list(list_id);
            ExcessPropertiesKind::Intersection(members.to_vec())
        }
        _ => ExcessPropertiesKind::NotObject,
    }
}

// =============================================================================
// Constructor Access Level Classification
// =============================================================================

/// Classification for checking constructor access level.
#[derive(Debug, Clone)]
pub enum ConstructorAccessKind {
    /// Ref or `TypeQuery` - resolve symbol
    SymbolRef(crate::types::SymbolRef),
    /// Application - check base
    Application(crate::types::TypeApplicationId),
    /// Not applicable
    Other,
}

/// Classify a type for constructor access level checking.
pub fn classify_for_constructor_access(
    db: &dyn TypeDatabase,
    type_id: TypeId,
) -> ConstructorAccessKind {
    let Some(key) = db.lookup(type_id) else {
        return ConstructorAccessKind::Other;
    };

    match key {
        TypeData::TypeQuery(sym_ref) => ConstructorAccessKind::SymbolRef(sym_ref),
        TypeData::Application(app_id) => ConstructorAccessKind::Application(app_id),
        _ => ConstructorAccessKind::Other,
    }
}

// =============================================================================
// Assignability Evaluation Classification
// =============================================================================

/// Classification for types that need evaluation before assignability.
#[derive(Debug, Clone)]
pub enum AssignabilityEvalKind {
    /// Application - evaluate with resolution
    Application,
    /// Index/KeyOf/Mapped/Conditional - evaluate with env
    NeedsEnvEval,
    /// Already resolved
    Resolved,
}

/// Classify a type for assignability evaluation.
pub fn classify_for_assignability_eval(
    db: &dyn TypeDatabase,
    type_id: TypeId,
) -> AssignabilityEvalKind {
    let Some(key) = db.lookup(type_id) else {
        return AssignabilityEvalKind::Resolved;
    };

    match key {
        TypeData::Application(_) | TypeData::Lazy(_) => AssignabilityEvalKind::Application,
        TypeData::IndexAccess(_, _)
        | TypeData::KeyOf(_)
        | TypeData::Mapped(_)
        | TypeData::Conditional(_) => AssignabilityEvalKind::NeedsEnvEval,
        _ => AssignabilityEvalKind::Resolved,
    }
}

// =============================================================================
// Binding Element Type Classification
// =============================================================================

/// Classification for binding element (destructuring) type extraction.
#[derive(Debug, Clone)]
pub enum BindingElementTypeKind {
    /// Array type - use element type
    Array(TypeId),
    /// Tuple type - use element by index
    Tuple(crate::types::TupleListId),
    /// Object type - use property type
    Object(crate::types::ObjectShapeId),
    /// Not applicable
    Other,
}

/// Classify a type for binding element type extraction.
pub fn classify_for_binding_element(
    db: &dyn TypeDatabase,
    type_id: TypeId,
) -> BindingElementTypeKind {
    let Some(key) = db.lookup(type_id) else {
        return BindingElementTypeKind::Other;
    };

    match key {
        TypeData::Array(elem) => BindingElementTypeKind::Array(elem),
        TypeData::Tuple(list_id) => BindingElementTypeKind::Tuple(list_id),
        TypeData::Object(shape_id) => BindingElementTypeKind::Object(shape_id),
        _ => BindingElementTypeKind::Other,
    }
}

// =============================================================================
// Additional Accessor Helpers
// =============================================================================

/// Get the `DefId` from a Lazy type.
pub fn get_lazy_def_id(db: &dyn TypeDatabase, type_id: TypeId) -> Option<crate::def::DefId> {
    match db.lookup(type_id) {
        Some(TypeData::Lazy(def_id)) => Some(def_id),
        _ => None,
    }
}

/// Get the `DefId` from a Lazy type.
pub fn get_def_id(db: &dyn TypeDatabase, type_id: TypeId) -> Option<crate::def::DefId> {
    match db.lookup(type_id) {
        Some(TypeData::Lazy(def_id)) => Some(def_id),
        _ => None,
    }
}

/// Get the `DefId` from a Lazy type.
/// Returns (Option<SymbolRef>, Option<DefId>) - `DefId` will be Some for Lazy types.
pub fn get_type_identity(
    db: &dyn TypeDatabase,
    type_id: TypeId,
) -> (Option<crate::types::SymbolRef>, Option<crate::def::DefId>) {
    match db.lookup(type_id) {
        Some(TypeData::Lazy(def_id)) => (None, Some(def_id)),
        _ => (None, None),
    }
}

/// Get the enum components (`DefId` and member type) if the type is an Enum type.
///
/// Returns `Some((def_id, member_type))` where:
/// - `def_id` is the unique identity of the enum for nominal checking
/// - `member_type` is the structural union of member types (e.g., 0 | 1)
pub fn get_enum_components(
    db: &dyn TypeDatabase,
    type_id: TypeId,
) -> Option<(crate::def::DefId, TypeId)> {
    match db.lookup(type_id) {
        Some(TypeData::Enum(def_id, member_type)) => Some((def_id, member_type)),
        _ => None,
    }
}

/// Get the mapped type ID if the type is a Mapped type.
pub fn get_mapped_type_id(
    db: &dyn TypeDatabase,
    type_id: TypeId,
) -> Option<crate::types::MappedTypeId> {
    match db.lookup(type_id) {
        Some(TypeData::Mapped(mapped_id)) => Some(mapped_id),
        _ => None,
    }
}

/// Get the conditional type ID if the type is a Conditional type.
pub fn get_conditional_type_id(
    db: &dyn TypeDatabase,
    type_id: TypeId,
) -> Option<crate::types::ConditionalTypeId> {
    match db.lookup(type_id) {
        Some(TypeData::Conditional(cond_id)) => Some(cond_id),
        _ => None,
    }
}

/// Get the keyof inner type if the type is a `KeyOf` type.
pub fn get_keyof_inner(db: &dyn TypeDatabase, type_id: TypeId) -> Option<TypeId> {
    match db.lookup(type_id) {
        Some(TypeData::KeyOf(inner)) => Some(inner),
        _ => None,
    }
}

// =============================================================================
// Symbol Resolution Traversal Classification
// =============================================================================

/// Classification for traversing types to resolve symbols.
/// Used by `ensure_application_symbols_resolved_inner`.
#[derive(Debug, Clone)]
pub enum SymbolResolutionTraversalKind {
    /// Application type - resolve base symbol and recurse
    Application {
        app_id: crate::types::TypeApplicationId,
        base: TypeId,
        args: Vec<TypeId>,
    },
    /// Lazy(DefId) type - resolve via `DefId`
    Lazy(crate::def::DefId),
    /// Type parameter - recurse into constraint/default
    TypeParameter {
        constraint: Option<TypeId>,
        default: Option<TypeId>,
    },
    /// Union or Intersection - recurse into members
    Members(Vec<TypeId>),
    /// Function type - recurse into signature components
    Function(crate::types::FunctionShapeId),
    /// Callable type - recurse into signatures
    Callable(crate::types::CallableShapeId),
    /// Object type - recurse into properties and index signatures
    Object(crate::types::ObjectShapeId),
    /// Array type - recurse into element
    Array(TypeId),
    /// Tuple type - recurse into elements
    Tuple(crate::types::TupleListId),
    /// Conditional type - recurse into all branches
    Conditional(crate::types::ConditionalTypeId),
    /// Mapped type - recurse into constraint, template, `name_type`
    Mapped(crate::types::MappedTypeId),
    /// Readonly wrapper - recurse into inner
    Readonly(TypeId),
    /// Index access - recurse into both types
    IndexAccess { object: TypeId, index: TypeId },
    /// `KeyOf` - recurse into inner
    KeyOf(TypeId),
    /// Terminal type - no further traversal needed
    Terminal,
}

/// Classify a type for symbol resolution traversal.
pub fn classify_for_symbol_resolution_traversal(
    db: &dyn TypeDatabase,
    type_id: TypeId,
) -> SymbolResolutionTraversalKind {
    let Some(key) = db.lookup(type_id) else {
        return SymbolResolutionTraversalKind::Terminal;
    };

    match key {
        TypeData::Application(app_id) => {
            let app = db.type_application(app_id);
            SymbolResolutionTraversalKind::Application {
                app_id,
                base: app.base,
                args: app.args.clone(),
            }
        }
        TypeData::Lazy(def_id) => SymbolResolutionTraversalKind::Lazy(def_id),
        TypeData::TypeParameter(param) | TypeData::Infer(param) => {
            SymbolResolutionTraversalKind::TypeParameter {
                constraint: param.constraint,
                default: param.default,
            }
        }
        TypeData::Union(members_id) | TypeData::Intersection(members_id) => {
            let members = db.type_list(members_id);
            SymbolResolutionTraversalKind::Members(members.to_vec())
        }
        TypeData::Function(shape_id) => SymbolResolutionTraversalKind::Function(shape_id),
        TypeData::Callable(shape_id) => SymbolResolutionTraversalKind::Callable(shape_id),
        TypeData::Object(shape_id) | TypeData::ObjectWithIndex(shape_id) => {
            SymbolResolutionTraversalKind::Object(shape_id)
        }
        TypeData::Array(elem) => SymbolResolutionTraversalKind::Array(elem),
        TypeData::Tuple(elems_id) => SymbolResolutionTraversalKind::Tuple(elems_id),
        TypeData::Conditional(cond_id) => SymbolResolutionTraversalKind::Conditional(cond_id),
        TypeData::Mapped(mapped_id) => SymbolResolutionTraversalKind::Mapped(mapped_id),
        TypeData::ReadonlyType(inner) => SymbolResolutionTraversalKind::Readonly(inner),
        TypeData::IndexAccess(obj, idx) => SymbolResolutionTraversalKind::IndexAccess {
            object: obj,
            index: idx,
        },
        TypeData::KeyOf(inner) => SymbolResolutionTraversalKind::KeyOf(inner),
        _ => SymbolResolutionTraversalKind::Terminal,
    }
}

// =============================================================================
// Interface Merge Type Classification
// =============================================================================

/// Classification for types when merging interfaces.
///
/// This enum provides a structured way to handle interface type merging,
/// abstracting away the internal `TypeData` representation. Used for merging
/// derived and base interface types.
#[derive(Debug, Clone)]
pub enum InterfaceMergeKind {
    /// Callable type with call/construct signatures and properties
    Callable(crate::types::CallableShapeId),
    /// Object type with properties only
    Object(crate::types::ObjectShapeId),
    /// Object type with properties and index signatures
    ObjectWithIndex(crate::types::ObjectShapeId),
    /// Intersection type - create intersection with base
    Intersection,
    /// Other type kinds - return derived unchanged
    Other,
}

/// Classify a type for interface merging operations.
///
/// This function examines a type and returns information about how to handle it
/// when merging interface types. Used by `merge_interface_types`.
///
/// # Example
///
/// ```ignore
/// use crate::type_queries::{classify_for_interface_merge, InterfaceMergeKind};
///
/// match classify_for_interface_merge(&db, type_id) {
///     InterfaceMergeKind::Callable(shape_id) => {
///         let shape = db.callable_shape(shape_id);
///         // Merge signatures and properties
///     }
///     InterfaceMergeKind::Object(shape_id) => {
///         let shape = db.object_shape(shape_id);
///         // Merge properties only
///     }
///     InterfaceMergeKind::ObjectWithIndex(shape_id) => {
///         let shape = db.object_shape(shape_id);
///         // Merge properties and index signatures
///     }
///     InterfaceMergeKind::Intersection => {
///         // Create intersection with base type
///     }
///     InterfaceMergeKind::Other => {
///         // Return derived unchanged
///     }
/// }
/// ```
pub fn classify_for_interface_merge(db: &dyn TypeDatabase, type_id: TypeId) -> InterfaceMergeKind {
    let Some(key) = db.lookup(type_id) else {
        return InterfaceMergeKind::Other;
    };

    match key {
        TypeData::Callable(shape_id) => InterfaceMergeKind::Callable(shape_id),
        TypeData::Object(shape_id) => InterfaceMergeKind::Object(shape_id),
        TypeData::ObjectWithIndex(shape_id) => InterfaceMergeKind::ObjectWithIndex(shape_id),
        TypeData::Intersection(_) => InterfaceMergeKind::Intersection,
        // All other types cannot be structurally merged for interfaces
        TypeData::BoundParameter(_)
        | TypeData::Intrinsic(_)
        | TypeData::Literal(_)
        | TypeData::Union(_)
        | TypeData::Array(_)
        | TypeData::Tuple(_)
        | TypeData::Function(_)
        | TypeData::TypeParameter(_)
        | TypeData::Infer(_)
        | TypeData::Lazy(_)
        | TypeData::Recursive(_)
        | TypeData::Application(_)
        | TypeData::Conditional(_)
        | TypeData::Mapped(_)
        | TypeData::IndexAccess(_, _)
        | TypeData::KeyOf(_)
        | TypeData::TemplateLiteral(_)
        | TypeData::UniqueSymbol(_)
        | TypeData::ThisType
        | TypeData::TypeQuery(_)
        | TypeData::ReadonlyType(_)
        | TypeData::NoInfer(_)
        | TypeData::StringIntrinsic { .. }
        | TypeData::ModuleNamespace(_)
        | TypeData::Error
        | TypeData::Enum(_, _) => InterfaceMergeKind::Other,
    }
}

// =============================================================================
// Augmentation Target Classification
// =============================================================================

/// Classification for augmentation operations on types.
///
/// Similar to `InterfaceMergeKind` but specifically for module augmentation
/// where we merge additional properties into an existing type.
#[derive(Debug, Clone)]
pub enum AugmentationTargetKind {
    /// Object type - merge properties directly
    Object(crate::types::ObjectShapeId),
    /// Object with index signatures - preserve index signatures when merging
    ObjectWithIndex(crate::types::ObjectShapeId),
    /// Callable type - merge properties while preserving signatures
    Callable(crate::types::CallableShapeId),
    /// Other type - create new object with augmentation members
    Other,
}

/// Classify a type for augmentation operations.
///
/// This function examines a type and returns information about how to handle it
/// when applying module augmentations. Used by `apply_module_augmentations`.
pub fn classify_for_augmentation(db: &dyn TypeDatabase, type_id: TypeId) -> AugmentationTargetKind {
    let Some(key) = db.lookup(type_id) else {
        return AugmentationTargetKind::Other;
    };

    match key {
        TypeData::Object(shape_id) => AugmentationTargetKind::Object(shape_id),
        TypeData::ObjectWithIndex(shape_id) => AugmentationTargetKind::ObjectWithIndex(shape_id),
        TypeData::Callable(shape_id) => AugmentationTargetKind::Callable(shape_id),
        // All other types are treated as Other for augmentation
        _ => AugmentationTargetKind::Other,
    }
}