typespec_rs 0.4.3

A Rust implementation of the TypeSpec type system — parser, checker, and emitter
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
//! TypeSpec Checker - Core type checking for TypeSpec-Rust
//! Ported from TypeSpec compiler/src/core/checker.ts
//!
//! The checker is responsible for:
//! - Type checking and validation
//! - Type relationship verification (assignability)
//! - Constraint checking
//! - Error diagnostics
//! - Value vs Type distinction

// Module declarations
mod check_alias;
mod check_augment;
mod check_clone;
mod check_declarations;
mod check_decorators;
mod check_directives;
mod check_enum;
mod check_expressions;
mod check_helpers;
mod check_interface;
mod check_literals;
pub(crate) mod check_model;
mod check_namespace;
mod check_namespace_helpers;
mod check_operation;
mod check_program_flow;
mod check_reference_resolution;
mod check_scalar;
mod check_spread;
mod check_stdlib;
mod check_template;
mod check_template_instantiation;
mod check_union;
mod check_visibility;
pub mod decorator_utils;
mod helpers;
pub mod type_relation;
pub mod type_utils;
pub mod types;

// Re-exports
pub use decorator_utils::*;
pub use type_relation::{Related, TypeRelationChecker, TypeRelationError, TypeRelationErrorCode};
pub use type_utils::*;
pub use types::*;

// ============================================================================
// Local macros for common patterns
// ============================================================================

/// Get the AstBuilder reference or return early with the given value.
/// Usage: `let ast = require_ast_or!(self, self.error_type);`
/// Or without explicit return (defaults to `()`): `let ast = require_ast_or!(self);`
#[macro_export]
#[doc(hidden)]
macro_rules! require_ast_or {
    // Form without explicit return (defaults to ())
    ($self:expr) => {{
        match $self.require_ast() {
            Some(ast) => ast,
            None => return,
        }
    }};
    // Form with explicit return value
    ($self:expr, $ret:expr) => {{
        match $self.require_ast() {
            Some(ast) => ast,
            None => return $ret,
        }
    }};
}

/// Get the AstBuilder reference and extract a specific AST node variant.
/// Returns `(ast, node)` tuple. Use `let (_, node) = ...` if ast isn't needed.
/// Usage: `let (ast, node) = require_ast_node!(self, node_id, ModelStatement, self.error_type);`
/// Or without explicit return (defaults to `()`): `let (ast, node) = require_ast_node!(self, node_id, ConstStatement);`
#[macro_export]
#[doc(hidden)]
macro_rules! require_ast_node {
    // Form without explicit return value (defaults to ())
    ($self:expr, $node_id:expr, $variant:ident) => {{
        let ast = match $self.require_ast() {
            Some(ast) => ast,
            None => return,
        };
        let node = match ast.id_to_node($node_id) {
            Some($crate::parser::AstNode::$variant(decl)) => decl.clone(),
            _ => return,
        };
        (ast, node)
    }};
    // Form with explicit return value
    ($self:expr, $node_id:expr, $variant:ident, $ret:expr) => {{
        let ast = match $self.require_ast() {
            Some(ast) => ast,
            None => return $ret,
        };
        let node = match ast.id_to_node($node_id) {
            Some($crate::parser::AstNode::$variant(decl)) => decl.clone(),
            _ => return $ret,
        };
        (ast, node)
    }};
}

// ============================================================================
// Checker Imports
// ============================================================================

// Re-export macros for sub-modules (use super::*; will include these)
pub(crate) use crate::require_ast_node;
pub(crate) use crate::require_ast_or;

use crate::ast::node::NodeId;
use crate::ast::types::SyntaxKind;
use crate::diagnostics::Diagnostic;
use crate::modifiers::{self, ModifierFlags};
use crate::parser::{AstBuilder, AstNode};
use std::collections::{HashMap, HashSet};
use std::rc::Rc;

/// Concatenate two route path segments, handling leading/trailing slashes.
/// e.g., ("/chat", "/completions") → "/chat/completions"
fn format_route_path(base: &str, path: &str) -> String {
    let base = base.trim_end_matches('/');
    let path = path.trim_start_matches('/');
    if base.is_empty() {
        format!("/{}", path)
    } else if path.is_empty() {
        base.to_string()
    } else {
        format!("{}/{}", base, path)
    }
}

// ============================================================================
// Custom Decorator Registration
// ============================================================================

/// Definition of a custom decorator to be registered programmatically.
///
/// Used with [`Checker::register_decorator`] to add decorators without
/// source parsing (`extern dec`), avoiding keyword conflicts and type
/// resolution issues.
#[derive(Debug, Clone)]
pub struct CustomDecoratorDef {
    /// Decorator name (e.g. "command", "flag")
    pub name: String,
    /// Namespace name (e.g. "CLI", "MyApp")
    pub namespace: String,
    /// Target type constraint (e.g. "unknown", "Model", "Operation")
    pub target_type: String,
    /// Parameter definitions for the decorator.
    /// When empty, the decorator accepts any number of arguments (no validation).
    /// When non-empty, argument count and type checking are enforced.
    pub parameters: Vec<DecoratorParamDef>,
}

/// A parameter definition for a custom decorator.
#[derive(Debug, Clone)]
pub struct DecoratorParamDef {
    /// Parameter name
    pub name: String,
    /// Type name (e.g. "string", "int32", "unknown" to skip type checking)
    pub type_name: String,
    /// Whether this parameter is optional
    pub optional: bool,
    /// Whether this is a rest parameter (absorbs remaining args)
    pub rest: bool,
}

// ============================================================================
// Global decorator registry
// ============================================================================

use std::sync::RwLock;

/// Global decorator registry — persists across Checker instances.
/// Register once at startup, all new Checkers automatically pick them up.
static GLOBAL_DECORATORS: RwLock<Vec<CustomDecoratorDef>> = RwLock::new(Vec::new());

/// Register a custom decorator globally. All subsequent `Checker::new()` calls
/// will automatically include this decorator.
///
/// Duplicate registrations (same name + namespace) are silently ignored.
///
/// # Example
///
/// ```ignore
/// // Call once at startup
/// typespec_rs::checker::register_global_decorator("route", "HTTP", "Operation");
/// typespec_rs::checker::register_global_decorator("tag", "API", "unknown");
///
/// // Every new Checker automatically has these decorators
/// let mut checker = typespec_rs::checker::Checker::new();
/// // No need to call checker.register_decorator() again!
/// ```
pub fn register_global_decorator(name: &str, namespace: &str, target_type: &str) {
    register_global_decorator_with_params(name, namespace, target_type, Vec::new());
}

/// Register a custom decorator globally with parameter definitions.
/// All subsequent `Checker::new()` calls will automatically include this decorator.
pub fn register_global_decorator_with_params(
    name: &str,
    namespace: &str,
    target_type: &str,
    parameters: Vec<DecoratorParamDef>,
) {
    if let Ok(mut registry) = GLOBAL_DECORATORS.write()
        && !registry
            .iter()
            .any(|d| d.name == name && d.namespace == namespace)
    {
        registry.push(CustomDecoratorDef {
            name: name.to_string(),
            namespace: namespace.to_string(),
            target_type: target_type.to_string(),
            parameters,
        });
    }
}

/// Bulk-register custom decorators globally.
///
/// Duplicate registrations (same name + namespace) are silently ignored.
pub fn register_global_decorators(decorators: Vec<(&str, &str, &str)>) {
    if let Ok(mut registry) = GLOBAL_DECORATORS.write() {
        for (name, namespace, target_type) in decorators {
            if !registry
                .iter()
                .any(|d| d.name == name && d.namespace == namespace)
            {
                registry.push(CustomDecoratorDef {
                    name: name.to_string(),
                    namespace: namespace.to_string(),
                    target_type: target_type.to_string(),
                    parameters: Vec::new(),
                });
            }
        }
    }
}

/// Returns the globally registered decorators (cloned).
fn get_global_decorators() -> Vec<CustomDecoratorDef> {
    GLOBAL_DECORATORS
        .read()
        .map(|r| r.clone())
        .unwrap_or_default()
}

// ============================================================================
// Check Context and Flags
// ============================================================================

/// Checker flags for controlling checking behavior
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CheckFlags {
    None = 0,
    /// Currently checking within an uninstantiated template declaration
    InTemplateDeclaration = 1 << 0,
}

/// Re-export TypeMapper from types module
pub use crate::checker::types::TypeMapper;

/// Check context for tracking type mapping during checking
#[derive(Debug, Clone)]
pub struct CheckContext {
    /// The type mapper associated with this context, if any
    pub mapper: Option<TypeMapper>,
    /// The flags enabled in this context
    pub flags: CheckFlags,
}

impl CheckContext {
    /// Create a default check context
    pub fn new() -> Self {
        Self {
            mapper: None,
            flags: CheckFlags::None,
        }
    }

    /// Create a check context with a mapper
    pub fn with_mapper(mapper: Option<TypeMapper>) -> Self {
        Self {
            mapper,
            flags: CheckFlags::None,
        }
    }

    /// Create a new context with the given flags added
    pub fn with_flags(&self, flags: CheckFlags) -> Self {
        Self {
            mapper: self.mapper.clone(),
            flags,
        }
    }

    /// Check if ALL of the given flags are enabled
    pub fn has_flags(&self, flags: CheckFlags) -> bool {
        (self.flags as u32 & flags as u32) == flags as u32
    }
}

impl Default for CheckContext {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Symbol Links
// ============================================================================

/// Symbol links for tracking type information per symbol
#[derive(Debug, Clone, Default)]
pub struct SymbolLinks {
    /// Declared type for the symbol
    pub declared_type: Option<TypeId>,
    /// Type for the symbol
    pub type_id: Option<TypeId>,
    /// Instantiation map for template types
    /// Key: vector of TypeIds (template arguments), Value: instantiated TypeId
    pub instantiations: Option<HashMap<Vec<TypeId>, TypeId>>,
    /// Whether this node resolved to a template instantiation (e.g., `Foo<string>`)
    /// Ported from TS NodeLinks.isTemplateInstantiation
    pub is_template_instantiation: bool,
}

// ============================================================================
// Checker
// ============================================================================

/// The main checker struct
pub struct Checker {
    // ---- Type/Value stores ----
    /// Type store for all created types
    pub type_store: TypeStore,
    /// Value store for all created values
    pub value_store: ValueStore,

    // ---- Parse result ----
    /// AST builder from parser
    ast: Option<Rc<AstBuilder>>,
    /// Root node ID from parse result
    pub root_id: NodeId,

    // ---- Well-known types ----
    /// ErrorType intrinsic TypeId
    pub error_type: TypeId,
    /// Void intrinsic TypeId
    pub void_type: TypeId,
    /// Never intrinsic TypeId
    pub never_type: TypeId,
    /// Null intrinsic TypeId
    pub null_type: TypeId,
    /// Unknown intrinsic TypeId
    pub unknown_type: TypeId,

    // ---- Namespace tracking ----
    /// Global namespace TypeId
    pub global_namespace_type: Option<TypeId>,
    /// Current namespace stack during checking
    pub current_namespace: Option<TypeId>,
    /// TypeSpec namespace TypeId (the "TypeSpec" namespace containing stdlib types)
    pub typespec_namespace_id: Option<TypeId>,

    // ---- Declaration maps ----
    /// Map from name to TypeId for all declared types
    pub declared_types: HashMap<String, TypeId>,
    /// Map from name to ValueId for all declared values (const declarations)
    pub declared_values: HashMap<String, ValueId>,

    // ---- Node-to-type/value mapping ----
    /// Map from AST NodeId to TypeId
    pub node_type_map: HashMap<NodeId, TypeId>,
    /// Map from AST NodeId to ValueId
    pub node_value_map: HashMap<NodeId, ValueId>,

    // ---- Symbol links ----
    /// Symbol links per node
    pub symbol_links: HashMap<NodeId, SymbolLinks>,

    // ---- Standard types ----
    /// Standard TypeSpec scalar types (string, int32, etc.)
    pub std_types: HashMap<String, TypeId>,

    // ---- Diagnostics ----
    /// Collected diagnostics
    diagnostics_list: Vec<Diagnostic>,

    // ---- Circular reference detection ----
    /// Names currently being checked for circular base type references
    pub pending_base_type_names: HashSet<String>,
    /// Nodes currently being type-checked
    pub pending_type_checks: HashSet<NodeId>,
    /// Names currently being type-checked (for circular alias/type detection by name)
    pub pending_type_names: HashSet<String>,
    /// Names of operations currently being checked for circular `is` references
    pub pending_op_signature_names: HashSet<String>,
    /// Template parameter names currently being checked (for circular constraint detection)
    pub pending_template_constraint_names: HashSet<String>,
    /// Recursion depth counter for safety
    pub check_depth: u32,

    // ---- Type relation ----
    /// Type relation checker
    pub type_relation: TypeRelationChecker,

    // ---- Deprecation tracking ----
    /// Deprecation tracker
    pub deprecation_tracker: crate::deprecation::DeprecationTracker,

    /// Map from declaration NodeId to diagnostic codes suppressed by #suppress directives
    pub suppressed_diagnostics: HashMap<NodeId, Vec<String>>,

    /// Set of NodeIds for which directives have already been processed
    pub directives_processed: HashSet<NodeId>,

    // ---- Value exact type tracking ----
    /// Map from ValueId to exact TypeId (literal type) for values
    pub value_exact_types: HashMap<ValueId, TypeId>,

    // ---- Internal visibility tracking ----
    /// Set of TypeIds that are declared with the `internal` modifier
    pub internal_declarations: HashSet<TypeId>,

    // ---- Template parameter scope ----
    /// Stack of template parameter scopes (each scope maps name to NodeId)
    pub template_param_scope: Vec<HashMap<String, NodeId>>,

    // ---- Unused using tracking ----
    /// List of (NodeId, namespace_name) for all using declarations
    pub using_declarations: Vec<(NodeId, String)>,
    /// Set of namespace names that have been used (referenced during type resolution)
    pub used_using_names: HashSet<String>,

    // ---- Literal type interning ----
    /// Cache for string literal types (value -> TypeId) to ensure same-value literals share TypeId
    pub string_literal_cache: HashMap<String, TypeId>,
    /// Cache for numeric literal types (value_as_string -> TypeId) to ensure same-value literals share TypeId
    pub numeric_literal_cache: HashMap<String, TypeId>,
    /// Cache for boolean literal types (value -> TypeId) to ensure same-value booleans share TypeId
    pub boolean_literal_cache: HashMap<bool, TypeId>,

    // ---- Spread property tracking ----
    /// Map from parent model TypeId to list of child model TypeIds whose spreads are pending
    pub pending_spreads: HashMap<TypeId, Vec<TypeId>>,
    /// Map from source model TypeId to list of target model TypeIds that spread from it
    pub spread_sources: HashMap<TypeId, Vec<TypeId>>,

    // ---- Const circular reference detection ----
    /// Set of NodeIds for const statements currently being checked (for circular detection)
    pub pending_const_checks: HashSet<NodeId>,

    // ---- State accessors for decorator state ----
    /// State maps for intrinsic decorator data (@minValue, @maxValue, etc.)
    pub state_accessors: crate::state_accessors::StateAccessors,

    // ---- Custom decorator registration ----
    /// Custom decorators registered via `register_decorator()`, processed during `check_program()`
    pub custom_decorators: Vec<CustomDecoratorDef>,
}

// ============================================================================
// ResolvedModelProperty — fully resolved property info for downstream consumers
// ============================================================================

/// Fully resolved property constraints extracted from TypeSpec decorators
/// and stored in the state_accessors during type checking.
#[derive(Debug, Clone, Default)]
pub struct PropertyConstraints {
    pub min_value: Option<f64>,
    pub max_value: Option<f64>,
    pub exclusive_min: bool,
    pub exclusive_max: bool,
    pub min_length: Option<u32>,
    pub max_length: Option<u32>,
    pub pattern: Option<String>,
    pub format: Option<String>,
}

/// HTTP parameter location, derived from `@path`, `@query`, `@header`, `@body` etc.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HttpLocation {
    Path,
    Query,
    Header,
    Cookie,
    Body,
    BodyRoot,
    MultipartBody,
}

/// Fully resolved model property — a flat, easy-to-consume representation
/// of a `ModelPropertyType` with all decorator-derived information extracted.
///
/// This is the primary data structure for downstream consumers (emitters,
/// MCP adapters, HTTP gateways) that need parameter metadata without
/// navigating the checker's internal type graph.
#[derive(Debug, Clone)]
pub struct ResolvedModelProperty {
    pub name: String,
    /// Resolved type name: "string", "int32", "boolean", "object", "array", etc.
    pub type_name: String,
    pub optional: bool,
    /// Default value from `prop = value` syntax
    pub default_value: Option<DecoratorMarshalledValue>,
    pub doc: Option<String>,
    pub enum_values: Option<Vec<String>>,
    pub constraints: PropertyConstraints,
    pub http_location: Option<HttpLocation>,
    /// Nested properties for object types
    pub properties: Vec<ResolvedModelProperty>,
    /// Element descriptor for array types
    pub items: Option<Box<ResolvedModelProperty>>,
    pub deprecated: bool,
}

impl Checker {
    /// Create a new checker with empty stores, initializing well-known intrinsic types.
    pub fn new() -> Self {
        let mut type_store = TypeStore::new();
        let value_store = ValueStore::new();

        // Create intrinsic types
        let error_type = type_store.add(Type::Intrinsic(IntrinsicType {
            id: type_store.next_type_id(),
            name: IntrinsicTypeName::ErrorType,
            node: None,
            is_finished: true,
        }));
        let void_type = type_store.add(Type::Intrinsic(IntrinsicType {
            id: type_store.next_type_id(),
            name: IntrinsicTypeName::Void,
            node: None,
            is_finished: true,
        }));
        let never_type = type_store.add(Type::Intrinsic(IntrinsicType {
            id: type_store.next_type_id(),
            name: IntrinsicTypeName::Never,
            node: None,
            is_finished: true,
        }));
        let null_type = type_store.add(Type::Intrinsic(IntrinsicType {
            id: type_store.next_type_id(),
            name: IntrinsicTypeName::Null,
            node: None,
            is_finished: true,
        }));
        let unknown_type = type_store.add(Type::Intrinsic(IntrinsicType {
            id: type_store.next_type_id(),
            name: IntrinsicTypeName::Unknown,
            node: None,
            is_finished: true,
        }));

        Self {
            type_store,
            value_store,
            ast: None,
            root_id: 0,
            error_type,
            void_type,
            never_type,
            null_type,
            unknown_type,
            global_namespace_type: None,
            current_namespace: None,
            typespec_namespace_id: None,
            declared_types: HashMap::new(),
            declared_values: HashMap::new(),
            node_type_map: HashMap::new(),
            node_value_map: HashMap::new(),
            symbol_links: HashMap::new(),
            std_types: HashMap::new(),
            diagnostics_list: Vec::new(),
            pending_base_type_names: HashSet::new(),
            pending_type_checks: HashSet::new(),
            pending_type_names: HashSet::new(),
            pending_op_signature_names: HashSet::new(),
            pending_template_constraint_names: HashSet::new(),
            check_depth: 0,
            type_relation: TypeRelationChecker::new(),
            deprecation_tracker: crate::deprecation::DeprecationTracker::new(),
            suppressed_diagnostics: HashMap::new(),
            directives_processed: HashSet::new(),
            value_exact_types: HashMap::new(),
            internal_declarations: HashSet::new(),
            template_param_scope: Vec::new(),
            using_declarations: Vec::new(),
            used_using_names: HashSet::new(),
            string_literal_cache: HashMap::new(),
            numeric_literal_cache: HashMap::new(),
            boolean_literal_cache: HashMap::new(),
            pending_spreads: HashMap::new(),
            spread_sources: HashMap::new(),
            pending_const_checks: HashSet::new(),
            state_accessors: crate::state_accessors::StateAccessors::new(),
            custom_decorators: get_global_decorators(),
        }
    }

    /// Set the parse result from the parser
    pub fn set_parse_result(&mut self, root_id: NodeId, builder: AstBuilder) {
        self.root_id = root_id;
        self.ast = Some(Rc::new(builder));
    }

    // ========================================================================
    // Type/Value store access
    // ========================================================================

    /// Get a type by TypeId
    pub fn get_type(&self, id: TypeId) -> Option<&Type> {
        self.type_store.get(id)
    }

    /// Get a mutable reference to a type by TypeId
    pub fn get_type_mut(&mut self, id: TypeId) -> Option<&mut Type> {
        self.type_store.get_mut(id)
    }

    /// Get a value by ValueId
    pub fn get_value(&self, id: ValueId) -> Option<&Value> {
        self.value_store.get(id)
    }

    /// Get a mutable reference to a value by ValueId
    pub fn get_value_mut(&mut self, id: ValueId) -> Option<&mut Value> {
        self.value_store.get_mut(id)
    }

    /// Set the type of a value
    pub fn set_value_type(&mut self, value_id: ValueId, new_type: TypeId) {
        if let Some(v) = self.value_store.get_mut(value_id) {
            v.set_value_type(new_type);
        }
    }

    /// Allocate the next TypeId
    pub fn next_type_id(&self) -> TypeId {
        self.type_store.next_type_id()
    }

    /// Allocate the next ValueId
    pub fn next_value_id(&self) -> ValueId {
        self.value_store.next_value_id()
    }

    /// Create a new type and add it to the store
    pub fn create_type(&mut self, t: Type) -> TypeId {
        self.type_store.add(t)
    }

    /// Register a custom decorator programmatically.
    ///
    /// The decorator will be created during `check_program()` in the specified
    /// namespace. If the namespace doesn't exist, it will be created as a
    /// sub-namespace of the global namespace.
    ///
    /// This bypasses source parsing (`extern dec`), avoiding:
    /// - Keyword conflicts (e.g. `flag`, `arg`, `env` are reserved words)
    /// - Cross-namespace type resolution failures
    /// - Union type parameter syntax limitations
    ///
    /// Must be called **before** `check_program()`.
    pub fn register_decorator(&mut self, name: &str, namespace: &str, target_type: &str) {
        self.register_decorator_with_params(name, namespace, target_type, Vec::new());
    }

    /// Register a custom decorator with parameter definitions.
    ///
    /// When `parameters` is empty, the decorator accepts any number of arguments
    /// (no count validation). When non-empty, argument count and type checking
    /// are enforced during `check_and_store_decorators`.
    pub fn register_decorator_with_params(
        &mut self,
        name: &str,
        namespace: &str,
        target_type: &str,
        parameters: Vec<DecoratorParamDef>,
    ) {
        self.custom_decorators.push(CustomDecoratorDef {
            name: name.to_string(),
            namespace: namespace.to_string(),
            target_type: target_type.to_string(),
            parameters,
        });
    }

    /// Register multiple custom decorators at once.
    ///
    /// Each tuple is `(name, namespace, target_type)`.
    /// See [`register_decorator`](Self::register_decorator) for details.
    pub fn register_decorators(&mut self, decorators: Vec<(&str, &str, &str)>) {
        for (name, namespace, target_type) in decorators {
            self.register_decorator(name, namespace, target_type);
        }
    }

    /// Create a new value and add it to the store
    pub fn create_value(&mut self, v: Value) -> ValueId {
        self.value_store.add(v)
    }

    // ========================================================================
    // Diagnostics
    // ========================================================================

    /// Add a diagnostic
    pub fn add_diagnostic(&mut self, diag: Diagnostic) {
        self.diagnostics_list.push(diag);
    }

    /// Add an error diagnostic (convenience shorthand)
    pub(crate) fn error(&mut self, code: &str, msg: &str) {
        self.add_diagnostic(Diagnostic::error(code, msg));
    }

    /// Add a warning diagnostic (convenience shorthand)
    pub(crate) fn warning(&mut self, code: &str, msg: &str) {
        self.add_diagnostic(Diagnostic::warning(code, msg));
    }

    /// Get a human-readable string for a type (convenience shorthand)
    pub(crate) fn type_to_string(&self, type_id: TypeId) -> String {
        type_utils::type_to_string(&self.type_store, type_id)
    }

    /// Report an "unassignable" or "invalid-argument" type error
    pub(crate) fn error_unassignable(&mut self, code: &str, source: TypeId, target: TypeId) {
        self.error(
            code,
            &format!(
                "Type '{}' is not assignable to type '{}'",
                self.type_to_string(source),
                self.type_to_string(target),
            ),
        );
    }

    /// Get all diagnostics
    pub fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics_list
    }

    // ========================================================================
    // Literal type creation
    // ========================================================================

    /// Create an anonymous union type from a list of option types.
    /// Each option becomes a UnionVariant. The union is marked as `expression: true`
    /// (anonymous) and is immediately finished.
    /// Ported from TS checker createUnion().
    pub fn create_union(&mut self, options: Vec<TypeId>) -> TypeId {
        // Empty union → create a Union type with expression=true (represents never)
        if options.is_empty() {
            let mut u = UnionType::new(0, String::new(), None, None, true);
            u.is_finished = true;
            return self.create_type(Type::Union(u));
        }

        for &opt in &options {
            if opt == self.error_type {
                return self.error_type;
            }
        }

        // Create variant types first (with placeholder union=0)
        let mut variant_ids = Vec::new();
        let mut variant_names = Vec::new();
        for (i, &opt) in options.iter().enumerate() {
            let name = format!("v{}", i);
            let variant_id = self.create_type(Type::UnionVariant(UnionVariantType {
                id: 0, // will be corrected by create_type
                name: name.clone(),
                node: None,
                r#type: opt,
                union: None, // will be backfilled
                decorators: Vec::new(),
                doc: None,
                summary: None,
                is_finished: true,
            }));
            variant_ids.push(variant_id);
            variant_names.push(name);
        }

        // Create the union type
        let mut variant_map = HashMap::new();
        for (i, &vid) in variant_ids.iter().enumerate() {
            variant_map.insert(variant_names[i].clone(), vid);
        }
        let union_id = {
            let mut u = UnionType::new(0, String::new(), None, None, true);
            u.variants = variant_map;
            u.variant_names = variant_names;
            u.is_finished = true;
            self.create_type(Type::Union(u))
        };

        // Backfill union field on variants
        for &vid in &variant_ids {
            if let Some(Type::UnionVariant(v)) = self.get_type_mut(vid) {
                v.union = Some(union_id);
            }
        }

        union_id
    }

    /// Create a string literal type with caching.
    /// Same values always return the same TypeId (interning).
    /// Ported from TS checker createLiteralType().
    pub fn create_literal_type_string(&mut self, value: String) -> TypeId {
        if let Some(&cached) = self.string_literal_cache.get(&value) {
            return cached;
        }
        let type_id = self.create_type(Type::String(StringType {
            id: self.next_type_id(),
            value: value.clone(),
            node: None,
            is_finished: true,
        }));
        self.string_literal_cache.insert(value, type_id);
        type_id
    }

    /// Create a numeric literal type with caching.
    /// Ported from TS checker createLiteralType(value: number).
    pub fn create_literal_type_number(&mut self, value: f64, value_as_string: String) -> TypeId {
        if let Some(&cached) = self.numeric_literal_cache.get(&value_as_string) {
            return cached;
        }
        let type_id = self.create_type(Type::Number(NumericType {
            id: self.next_type_id(),
            value,
            value_as_string: value_as_string.clone(),
            node: None,
            is_finished: true,
        }));
        self.numeric_literal_cache.insert(value_as_string, type_id);
        type_id
    }

    /// Create a boolean literal type with caching.
    /// Ported from TS checker createLiteralType(value: boolean).
    pub fn create_literal_type_boolean(&mut self, value: bool) -> TypeId {
        if let Some(&cached) = self.boolean_literal_cache.get(&value) {
            return cached;
        }
        let type_id = self.create_type(Type::Boolean(BooleanType {
            id: self.next_type_id(),
            value,
            node: None,
            is_finished: true,
        }));
        self.boolean_literal_cache.insert(value, type_id);
        type_id
    }

    // ========================================================================
    // AST access
    // ========================================================================

    /// Get a reference to the AST builder, returning None if not set.
    /// This replaces the repeated `let ast = match &self.ast { Some(a) => a.clone(), None => return }` pattern.
    pub(crate) fn require_ast(&self) -> Option<Rc<AstBuilder>> {
        self.ast.clone()
    }

    // ========================================================================
    // Node dispatch
    // ========================================================================

    /// Check a node and return its TypeId.
    /// Wraps check_node_impl with depth limiting and directive processing.
    pub fn check_node(&mut self, ctx: &CheckContext, node_id: NodeId) -> TypeId {
        // Safety: prevent infinite recursion with a depth limit
        self.check_depth += 1;
        if self.check_depth > 200 {
            self.check_depth -= 1;
            return self.error_type;
        }

        let result = self.check_node_impl(ctx, node_id);

        // Process directives attached to this node (e.g., #deprecated, #suppress)
        // Skip if already processed (some check functions like check_model process
        // directives early to set up deprecation context before checking children)
        if result != self.error_type && !self.directives_processed.contains(&node_id) {
            self.process_directives(node_id, result);
            self.directives_processed.insert(node_id);
        }

        self.check_depth -= 1;

        // Record the type in node_type_map
        self.node_type_map.insert(node_id, result);
        result
    }

    /// Check modifiers on a declaration node and emit appropriate diagnostics.
    /// Ported from TS modifiers.ts checkModifiers()
    pub(crate) fn check_modifiers_and_report(
        &mut self,
        node_id: NodeId,
        kind: SyntaxKind,
        modifiers: &[NodeId],
    ) {
        let ast = match &self.ast {
            Some(ast) => ast.clone(),
            None => return,
        };

        // Compute modifier flags from the modifier nodes
        let mut modifier_flags = ModifierFlags::None;
        for &mod_id in modifiers {
            if let Some(AstNode::Modifier(m)) = ast.id_to_node(mod_id) {
                modifier_flags = modifier_flags | modifiers::modifier_to_flag(m.kind);
            }
        }

        // Per TS: emit experimental-feature warning for any use of 'internal' modifier
        if modifier_flags.contains(ModifierFlags::Internal) {
            self.warning(
                "experimental-feature",
                "The 'internal' modifier is an experimental feature.",
            );
            // Track this declaration as internal for visibility checking
            if let Some(&type_id) = self.node_type_map.get(&node_id) {
                self.internal_declarations.insert(type_id);
            }
        }

        // Check modifier validity using the shared check_modifiers function
        let result = modifiers::check_modifiers(modifier_flags, kind);

        // Emit invalid-modifier diagnostics for disallowed modifiers
        for invalid in &result.invalid_modifiers {
            self.error(
                "invalid-modifier",
                &format!(
                    "Modifier '{}' is not allowed on {}.",
                    invalid,
                    modifiers::get_declaration_kind_text(kind)
                ),
            );
        }

        // Emit invalid-modifier diagnostics for missing required modifiers
        for missing in &result.missing_modifiers {
            self.error(
                "invalid-modifier",
                &format!(
                    "Modifier '{}' is required on {}.",
                    missing,
                    modifiers::get_declaration_kind_text(kind)
                ),
            );
        }
    }

    pub(crate) fn check_node_impl(&mut self, ctx: &CheckContext, node_id: NodeId) -> TypeId {
        let ast = require_ast_or!(self, self.error_type);

        let node = match ast.id_to_node(node_id) {
            Some(n) => n.clone(),
            None => return self.error_type,
        };

        match &node {
            AstNode::ModelDeclaration(decl) => {
                self.check_modifiers_and_report(
                    node_id,
                    SyntaxKind::ModelStatement,
                    &decl.modifiers,
                );
                self.check_model(ctx, node_id)
            }
            AstNode::ModelExpression(_) => self.check_model(ctx, node_id),
            AstNode::ScalarDeclaration(decl) => {
                self.check_modifiers_and_report(
                    node_id,
                    SyntaxKind::ScalarStatement,
                    &decl.modifiers,
                );
                self.check_scalar(ctx, node_id)
            }
            AstNode::InterfaceDeclaration(decl) => {
                self.check_modifiers_and_report(
                    node_id,
                    SyntaxKind::InterfaceStatement,
                    &decl.modifiers,
                );
                self.check_interface(ctx, node_id)
            }
            AstNode::EnumDeclaration(decl) => {
                self.check_modifiers_and_report(
                    node_id,
                    SyntaxKind::EnumStatement,
                    &decl.modifiers,
                );
                self.check_enum(ctx, node_id)
            }
            AstNode::UnionDeclaration(decl) => {
                self.check_modifiers_and_report(
                    node_id,
                    SyntaxKind::UnionStatement,
                    &decl.modifiers,
                );
                self.check_union(ctx, node_id)
            }
            AstNode::NamespaceDeclaration(decl) => {
                self.check_modifiers_and_report(
                    node_id,
                    SyntaxKind::NamespaceStatement,
                    &decl.modifiers,
                );
                self.check_namespace(ctx, node_id)
            }
            AstNode::OperationDeclaration(decl) => {
                self.check_modifiers_and_report(
                    node_id,
                    SyntaxKind::OperationStatement,
                    &decl.modifiers,
                );
                self.check_operation(ctx, node_id)
            }
            AstNode::AliasStatement(decl) => {
                self.check_modifiers_and_report(
                    node_id,
                    SyntaxKind::AliasStatement,
                    &decl.modifiers,
                );
                self.check_alias(ctx, node_id)
            }
            AstNode::ConstStatement(decl) => {
                self.check_modifiers_and_report(
                    node_id,
                    SyntaxKind::ConstStatement,
                    &decl.modifiers,
                );
                self.check_const(ctx, node_id);
                self.error_type // const returns a value, not a type
            }
            AstNode::DecoratorDeclaration(decl) => {
                self.check_modifiers_and_report(
                    node_id,
                    SyntaxKind::DecoratorDeclarationStatement,
                    &decl.modifiers,
                );
                self.check_decorator_declaration(ctx, node_id)
            }
            AstNode::UsingDeclaration(_) => {
                self.check_using(node_id);
                self.void_type
            }
            AstNode::ImportStatement(decl) => {
                // Report import-not-found for any import (we don't have multi-file support yet)
                let path_str = match ast.id_to_node(decl.path) {
                    Some(AstNode::StringLiteral(sl)) => sl.value.clone(),
                    _ => String::new(),
                };
                if !path_str.is_empty() {
                    self.error(
                        "import-not-found",
                        &format!("Cannot find import '{}'", path_str),
                    );
                }
                self.void_type
            }
            AstNode::AugmentDecoratorStatement(_) => {
                self.check_augment_decorator(ctx, node_id);
                self.void_type
            }
            AstNode::FunctionDeclaration(decl) => {
                self.check_modifiers_and_report(
                    node_id,
                    SyntaxKind::FunctionDeclarationStatement,
                    &decl.modifiers,
                );
                self.check_function_declaration(ctx, node_id)
            }
            // Expression types
            AstNode::StringLiteral(_) => self.check_string_literal(node_id),
            AstNode::NumericLiteral(_) => self.check_numeric_literal(node_id),
            AstNode::BooleanLiteral(_) => self.check_boolean_literal(node_id),
            AstNode::TypeReference(_) => self.check_type_reference(ctx, node_id),
            AstNode::ArrayExpression(_) => self.check_array_expression(ctx, node_id),
            AstNode::TupleExpression(_) => self.check_tuple_expression(ctx, node_id),
            AstNode::UnionExpression(_) => self.check_union_expression(ctx, node_id),
            AstNode::IntersectionExpression(_) => self.check_intersection_expression(ctx, node_id),
            AstNode::VoidKeyword(_) => self.void_type,
            AstNode::NeverKeyword(_) => self.never_type,
            AstNode::UnknownKeyword(_) => self.unknown_type,
            AstNode::ValueOfExpression(_) => self.check_valueof(ctx, node_id),
            AstNode::TypeOfExpression(_) => self.check_typeof(ctx, node_id),
            AstNode::StringTemplateExpression(_) => self.check_string_template(node_id),
            AstNode::CallExpression(_) => self.check_call_expression(ctx, node_id),
            AstNode::ObjectLiteral(_) => self.check_object_literal(ctx, node_id),
            AstNode::ArrayLiteral(_) => self.check_array_literal(ctx, node_id),
            AstNode::MemberExpression(_) => self.check_member_expression(ctx, node_id),
            AstNode::Identifier(_) => self.check_identifier(ctx, node_id),
            AstNode::DecoratorExpression(_) => self.void_type,
            AstNode::DirectiveExpression(_) => {
                self.check_directive(node_id);
                self.void_type
            }
            AstNode::Doc(_) | AstNode::DocText(_) => self.void_type,
            AstNode::LineComment(_)
            | AstNode::BlockComment(_)
            | AstNode::EmptyStatement(_)
            | AstNode::StringTemplateSpan(_) => self.void_type,
            AstNode::TemplateArgument(tmpl_arg) => {
                // Unwrap template argument: check the inner value node
                self.check_node(ctx, tmpl_arg.argument)
            }
            AstNode::StringTemplateHead(_)
            | AstNode::StringTemplateMiddle(_)
            | AstNode::StringTemplateTail(_) => self.check_string_template_part(node_id),
            _ => self.error_type,
        }
    }

    // ========================================================================
    // Public API
    // ========================================================================

    /// Check a node and return an Entity (Type, Value, MixedConstraint, or Indeterminate).
    /// Ported from TS checker.ts checkNode() which returns Type | Value | Indeterminate.
    pub fn check_node_entity(&mut self, ctx: &CheckContext, node_id: NodeId) -> Entity {
        let type_id = self.check_node(ctx, node_id);
        Entity::Type(type_id)
    }

    /// Convert an Entity to a TypeId.
    /// For Type entities, returns the TypeId directly.
    /// For Value entities, returns the value's type.
    /// For Indeterminate entities, returns the inner TypeId.
    pub fn entity_to_type_id(&self, entity: &Entity) -> TypeId {
        match entity {
            Entity::Type(id) => *id,
            Entity::Value(id) => self
                .get_value(*id)
                .map(|v| v.value_type())
                .unwrap_or(self.error_type),
            Entity::Indeterminate(id) => *id,
            Entity::MixedConstraint(mc) => mc.type_constraint.unwrap_or(self.error_type),
        }
    }

    /// Check if source type is assignable to target type.
    /// Returns (is_assignable, errors) tuple.
    /// Ported from TS checker.isTypeAssignableTo().
    pub fn is_type_assignable_to(
        &mut self,
        source: TypeId,
        target: TypeId,
        _diagnostic_target: TypeId,
    ) -> (bool, Vec<TypeRelationError>) {
        let result = self
            .type_relation
            .is_related_with_store(&self.type_store, source, target);
        (result.is_true(), Vec::new())
    }

    /// Get the effective model type for a type.
    /// If the type is a named model, returns it directly.
    /// If the type is an anonymous model, checks if all its properties come
    /// from a single named source model and returns that source.
    /// Ported from TS checker.getEffectiveModelType().
    pub fn get_effective_model_type(&self, type_id: TypeId) -> TypeId {
        self.get_effective_model_type_with_filter(type_id, None)
    }

    /// Get the effective model type for a type with a property filter.
    /// Ported from TS checker.getEffectiveModelType().
    pub fn get_effective_model_type_with_filter(
        &self,
        type_id: TypeId,
        filter: Option<&dyn Fn(&Type) -> bool>,
    ) -> TypeId {
        match self.get_type(type_id) {
            Some(Type::Model(m)) => {
                // Named model: return itself (even with filter, per TS behavior)
                if !m.name.is_empty() {
                    return type_id;
                }

                // Check if filter would remove any properties
                if let Some(f) = filter {
                    let needs_filter = m.properties.values().any(|&prop_id| {
                        !f(self
                            .get_type(prop_id)
                            .unwrap_or(&Type::Intrinsic(IntrinsicType {
                                id: 0,
                                name: IntrinsicTypeName::ErrorType,
                                node: None,
                                is_finished: true,
                            })))
                    });
                    if needs_filter {
                        // With filter removing properties, we can't create a new model
                        // from &self — return the input model as-is
                        return type_id;
                    }
                }

                // Anonymous model: try to find a named source model
                self.resolve_effective_model(type_id)
            }
            Some(Type::Union(u)) => {
                let variant_ids: Vec<TypeId> = u
                    .variant_names
                    .iter()
                    .filter_map(|n| u.variants.get(n).copied())
                    .collect();
                if variant_ids.iter().all(|&v| {
                    matches!(
                        self.get_type(self.resolve_alias_chain(v)),
                        Some(Type::Model(_))
                    )
                }) {
                    type_id
                } else {
                    self.error_type
                }
            }
            _ => type_id,
        }
    }

    /// Get the effective route for an operation by combining interface-level
    /// and operation-level `@route` decorators.
    /// Returns the concatenated path (e.g., "/chat" + "/completions" = "/chat/completions").
    pub fn get_effective_route(&self, op_id: TypeId) -> Option<String> {
        let op = match self.get_type(op_id) {
            Some(Type::Operation(op)) => op,
            _ => return None,
        };

        // Get operation's own @route
        let op_route = self.find_route_arg(&op.decorators);

        // Get interface's @route if operation is in an interface
        let iface_route = op
            .interface_
            .and_then(|id| self.get_type(id))
            .and_then(|t| match t {
                Type::Interface(iface) => Some(self.find_route_arg(&iface.decorators)),
                _ => None,
            })
            .flatten();

        match (iface_route, op_route) {
            (Some(base), Some(path)) => Some(format_route_path(&base, &path)),
            (Some(base), None) => Some(base),
            (None, Some(path)) => Some(path),
            (None, None) => None,
        }
    }

    /// Get all effective decorators for an operation, including those inherited
    /// from its containing interface.
    /// Returns a Vec of references: interface decorators first, then operation's own.
    pub fn get_effective_decorators(&self, op_id: TypeId) -> Vec<&DecoratorApplication> {
        let op = match self.get_type(op_id) {
            Some(Type::Operation(op)) => op,
            _ => return Vec::new(),
        };

        let mut result = Vec::new();

        // Add interface decorators first (inherited)
        if let Some(iface_id) = op.interface_
            && let Some(Type::Interface(iface)) = self.get_type(iface_id)
        {
            for dec in &iface.decorators {
                result.push(dec);
            }
        }

        // Then add operation's own decorators
        for dec in &op.decorators {
            result.push(dec);
        }

        result
    }

    /// Find the first string argument of a @route decorator in the given decorator list.
    fn find_route_arg(&self, decorators: &[DecoratorApplication]) -> Option<String> {
        for dec in decorators {
            let is_route = dec.definition.is_some_and(|def_id| {
                self.get_type(def_id)
                    .is_some_and(|t| matches!(t, Type::Decorator(dt) if dt.name == "route"))
            });
            if is_route
                && let Some(arg) = dec.args.first()
                && let Some(DecoratorMarshalledValue::String(s)) = &arg.js_value
            {
                return Some(s.clone());
            }
        }
        None
    }

    /// Resolve a ModelPropertyType into a flat, fully extracted representation.
    ///
    /// This method extracts:
    /// - Type name (scalar name like "string", "int32", or "object"/"array")
    /// - Default value from `prop = value` syntax
    /// - Doc, enum values, and all constraint decorators via `state_accessors`
    /// - HTTP parameter location (@path, @query, @header, @body)
    /// - Nested properties for object types, items for array types
    pub fn resolve_model_property(&self, prop_id: TypeId) -> Option<ResolvedModelProperty> {
        let prop = match self.get_type(prop_id)? {
            Type::ModelProperty(p) => p.clone(),
            _ => return None,
        };

        let (type_name, properties, items, enum_values) = self.resolve_type_details(prop.r#type);

        let default_value = prop
            .default_value
            .and_then(|dv_id| self.resolve_default_value(dv_id));

        let constraints = self.extract_constraints(prop_id, prop.r#type);
        let http_location = self.resolve_http_location(prop_id);
        let doc = self.extract_doc(prop_id, &prop);
        let deprecated = self.is_deprecated(prop_id);

        Some(ResolvedModelProperty {
            name: prop.name,
            type_name,
            optional: prop.optional,
            default_value,
            doc,
            enum_values,
            constraints,
            http_location,
            properties,
            items,
            deprecated,
        })
    }

    /// Resolve the type name, nested properties, items, and enum values for a TypeId.
    #[allow(clippy::type_complexity)]
    fn resolve_type_details(
        &self,
        type_id: TypeId,
    ) -> (
        String,
        Vec<ResolvedModelProperty>,
        Option<Box<ResolvedModelProperty>>,
        Option<Vec<String>>,
    ) {
        let Some(t) = self.get_type(type_id) else {
            return ("string".into(), Vec::new(), None, None);
        };

        match t {
            Type::Scalar(s) => (s.name.clone(), Vec::new(), None, None),
            Type::String(_) => ("string".into(), Vec::new(), None, None),
            Type::Number(_) => ("number".into(), Vec::new(), None, None),
            Type::Boolean(_) => ("boolean".into(), Vec::new(), None, None),
            Type::Enum(e) => {
                let names = Some(e.member_names.clone());
                ("string".into(), Vec::new(), None, names)
            }
            Type::Tuple(tuple) => {
                let items = tuple.values.first().map(|&elem_id| {
                    let (tn, _, _, _) = self.resolve_type_details(elem_id);
                    Box::new(ResolvedModelProperty {
                        name: "item".into(),
                        type_name: tn,
                        optional: false,
                        default_value: None,
                        doc: None,
                        enum_values: None,
                        constraints: PropertyConstraints::default(),
                        http_location: None,
                        properties: Vec::new(),
                        items: None,
                        deprecated: false,
                    })
                });
                ("array".into(), Vec::new(), items, None)
            }
            Type::Model(model) => {
                if model.indexer.is_some() {
                    let items = model.indexer.map(|(_, val_id)| {
                        let (tn, _, _, _) = self.resolve_type_details(val_id);
                        Box::new(ResolvedModelProperty {
                            name: "item".into(),
                            type_name: tn,
                            optional: false,
                            default_value: None,
                            doc: None,
                            enum_values: None,
                            constraints: PropertyConstraints::default(),
                            http_location: None,
                            properties: Vec::new(),
                            items: None,
                            deprecated: false,
                        })
                    });
                    return ("array".into(), Vec::new(), items, None);
                }

                let props: Vec<ResolvedModelProperty> = model
                    .property_names
                    .iter()
                    .filter_map(|name| {
                        let prop_id = model.properties.get(name)?;
                        self.resolve_model_property(*prop_id)
                    })
                    .collect();

                ("object".into(), props, None, None)
            }
            Type::Union(union) => {
                for vname in &union.variant_names {
                    if let Some(&vid) = union.variants.get(vname)
                        && let Some(Type::UnionVariant(v)) = self.get_type(vid)
                    {
                        return self.resolve_type_details(v.r#type);
                    }
                }
                ("string".into(), Vec::new(), None, None)
            }
            Type::Intrinsic(intrinsic) => {
                let name = match intrinsic.name {
                    IntrinsicTypeName::Void => "void",
                    _ => "string",
                };
                (name.into(), Vec::new(), None, None)
            }
            _ => ("string".into(), Vec::new(), None, None),
        }
    }

    /// Extract default value from a TypeId (string literal, number, boolean, enum member).
    fn resolve_default_value(&self, type_id: TypeId) -> Option<DecoratorMarshalledValue> {
        match self.get_type(type_id)? {
            Type::String(s) => Some(DecoratorMarshalledValue::String(s.value.clone())),
            Type::Number(n) => Some(DecoratorMarshalledValue::Number(n.value)),
            Type::Boolean(b) => Some(DecoratorMarshalledValue::Boolean(b.value)),
            Type::EnumMember(m) => Some(DecoratorMarshalledValue::String(m.name.clone())),
            _ => None,
        }
    }

    /// Extract property constraints from state_accessors (populated during checking).
    #[allow(clippy::field_reassign_with_default)]
    fn extract_constraints(&self, prop_id: TypeId, type_id: TypeId) -> PropertyConstraints {
        let state = &self.state_accessors;
        use crate::libs::compiler;

        let mut c = PropertyConstraints::default();

        // Primary source: state_accessors from the property itself
        c.min_value = compiler::get_min_value(state, prop_id);
        c.max_value = compiler::get_max_value(state, prop_id);
        c.exclusive_min = compiler::get_min_value_exclusive(state, prop_id).is_some();
        c.exclusive_max = compiler::get_max_value_exclusive(state, prop_id).is_some();
        c.min_length = compiler::get_min_length(state, prop_id).map(|v| v as u32);
        c.max_length = compiler::get_max_length(state, prop_id).map(|v| v as u32);
        c.pattern = compiler::get_pattern(state, prop_id);
        c.format = compiler::get_format(state, prop_id);

        // Fallback: check the type's state_accessors (e.g., constraints on Scalar)
        if c.min_value.is_none() {
            c.min_value = compiler::get_min_value(state, type_id);
            if c.min_value.is_some() && !c.exclusive_min {
                c.exclusive_min = compiler::get_min_value_exclusive(state, type_id).is_some();
            }
        }
        if c.max_value.is_none() {
            c.max_value = compiler::get_max_value(state, type_id);
            if c.max_value.is_some() && !c.exclusive_max {
                c.exclusive_max = compiler::get_max_value_exclusive(state, type_id).is_some();
            }
        }
        if c.min_length.is_none() {
            c.min_length = compiler::get_min_length(state, type_id).map(|v| v as u32);
        }
        if c.max_length.is_none() {
            c.max_length = compiler::get_max_length(state, type_id).map(|v| v as u32);
        }
        if c.pattern.is_none() {
            c.pattern = compiler::get_pattern(state, type_id);
        }
        if c.format.is_none() {
            c.format = compiler::get_format(state, type_id);
        }

        c
    }

    /// Resolve HTTP parameter location from state_accessors.
    fn resolve_http_location(&self, prop_id: TypeId) -> Option<HttpLocation> {
        let state = &self.state_accessors;
        use crate::libs::http;

        if http::is_path(state, prop_id) {
            Some(HttpLocation::Path)
        } else if http::is_header(state, prop_id) {
            Some(HttpLocation::Header)
        } else if http::is_query(state, prop_id) {
            Some(HttpLocation::Query)
        } else if http::is_cookie(state, prop_id) {
            Some(HttpLocation::Cookie)
        } else if http::is_body(state, prop_id) {
            Some(HttpLocation::Body)
        } else if http::is_body_root(state, prop_id) {
            Some(HttpLocation::BodyRoot)
        } else if http::is_multipart_body(state, prop_id) {
            Some(HttpLocation::MultipartBody)
        } else {
            None
        }
    }

    /// Extract doc string from state_accessors, falling back to the property's doc field.
    fn extract_doc(&self, prop_id: TypeId, prop: &ModelPropertyType) -> Option<String> {
        let state = &self.state_accessors;
        crate::libs::compiler::get_doc(state, prop_id).or(prop.doc.clone())
    }

    /// Resolve the effective model for an anonymous model by finding a named
    /// source model that has the same set of properties.
    /// Ported from TS getEffectiveModelType (anonymous model resolution).
    fn resolve_effective_model(&self, type_id: TypeId) -> TypeId {
        let m = match self.get_type(type_id) {
            Some(Type::Model(m)) => m,
            _ => return type_id,
        };

        // Anonymous model with base model shouldn't happen per TS assertion
        // but handle gracefully
        if m.base_model.is_some() {
            return type_id;
        }

        // Empty model: return itself
        if m.properties.is_empty() {
            return type_id;
        }

        // Find candidate named models that could be the source of every property
        // TS NOTE: "We depend on the order of that spread and intersect source
        // properties here, which is that we see properties sourced from derived
        // types before properties sourced from their base types."
        // So we iterate property_names (insertion order) not HashMap values.
        let mut candidates: Option<HashSet<TypeId>> = None;
        for name in &m.property_names {
            let &prop_id = match m.properties.get(name) {
                Some(id) => id,
                None => continue,
            };
            let sources = self.get_named_source_models(prop_id);
            let Some(sources) = sources else {
                // Unsourced property: no possible match
                return type_id;
            };

            match &mut candidates {
                None => {
                    candidates = Some(sources);
                }
                Some(cands) => {
                    // TS: addDerivedModels(sources, candidates)
                    // Adds derived types of candidates into sources if their base is in sources
                    let mut sources_mut = sources;
                    Self::add_derived_models(&mut sources_mut, cands, self);
                    // Remove candidates not common to this property's sources
                    cands.retain(|c| sources_mut.contains(c));
                }
            }
        }

        // Search for a candidate whose property count matches
        if let Some(cands) = candidates {
            let prop_count = m.properties.len();
            for &candidate_id in &cands {
                if let Some(count) = self.count_properties_inherited(candidate_id, None)
                    && prop_count == count
                {
                    return candidate_id;
                }
            }
        }

        type_id
    }

    /// Get the set of named source models for a property by following
    /// the source_property chain. Returns None if the property has no source
    /// (i.e., it's an original property, not copied from spread/intersection).
    /// Ported from TS getNamedSourceModels().
    fn get_named_source_models(&self, prop_id: TypeId) -> Option<HashSet<TypeId>> {
        let prop = match self.get_type(prop_id) {
            Some(Type::ModelProperty(p)) => p,
            _ => return None,
        };

        prop.source_property?;

        let mut set = HashSet::new();
        let mut current = Some(prop_id);
        while let Some(pid) = current {
            if let Some(Type::ModelProperty(p)) = self.get_type(pid) {
                if let Some(model_id) = p.model
                    && let Some(Type::Model(m)) = self.get_type(model_id)
                    && !m.name.is_empty()
                {
                    set.insert(model_id);
                }
                current = p.source_property;
            } else {
                break;
            }
        }

        if set.is_empty() { None } else { Some(set) }
    }

    /// Add derived types: for each element in `possibly_derived` that isn't
    /// already in `sources`, check if its base model chain includes any model
    /// in `sources`. If so, add the element to `sources`.
    /// Ported from TS addDerivedModels(sources, possiblyDerivedModels).
    fn add_derived_models(
        sources: &mut HashSet<TypeId>,
        possibly_derived: &HashSet<TypeId>,
        checker: &Checker,
    ) {
        for &element in possibly_derived {
            if !sources.contains(&element) {
                let mut current = if let Some(Type::Model(m)) = checker.get_type(element) {
                    m.base_model
                } else {
                    None
                };
                while let Some(tid) = current {
                    if sources.contains(&tid) {
                        sources.insert(element);
                        break;
                    }
                    if let Some(Type::Model(m)) = checker.get_type(tid) {
                        current = m.base_model;
                    } else {
                        break;
                    }
                }
            }
        }
    }

    /// Count all properties of a model including inherited (base model chain).
    /// Ported from TS countPropertiesInherited().
    fn count_properties_inherited(
        &self,
        type_id: TypeId,
        filter: Option<&dyn Fn(&Type) -> bool>,
    ) -> Option<usize> {
        let mut count = 0;
        let mut current: Option<TypeId> = Some(type_id);
        while let Some(mid) = current {
            if let Some(Type::Model(m)) = self.get_type(mid) {
                for &prop_id in m.properties.values() {
                    if let Some(f) = filter {
                        if let Some(prop_type) = self.get_type(prop_id)
                            && f(prop_type)
                        {
                            count += 1;
                        }
                    } else {
                        count += 1;
                    }
                }
                current = m.base_model;
            } else {
                return None;
            }
        }
        Some(count)
    }

    /// Resolve an alias chain: follow scalar base_scalar references until we reach
    /// a non-aliased type, detect a cycle, or encounter a Scalar→Scalar extends
    /// relationship (which should NOT be resolved through, as it represents
    /// "extends" not "alias").
    pub fn resolve_alias_chain(&self, type_id: TypeId) -> TypeId {
        let mut current = type_id;
        let mut seen = HashSet::new();
        loop {
            if !seen.insert(current) {
                return current;
            }
            match self.get_type(current) {
                Some(Type::Scalar(s)) if s.base_scalar.is_some() => {
                    let Some(base_id) = s.base_scalar else {
                        return current;
                    };
                    // If base is also a Scalar, this is an extends relationship
                    // (e.g., int32 extends int64), not an alias. Stop resolving.
                    // Aliases like `alias Foo = string | int32` have base_scalar
                    // pointing to non-Scalar types (Union, Model, etc.).
                    // Also stop if the alias name matches the base scalar name
                    // (which shouldn't happen but protects against edge cases).
                    if matches!(self.get_type(base_id), Some(Type::Scalar(base)) if base.name == s.name)
                    {
                        return current;
                    }
                    if matches!(self.get_type(base_id), Some(Type::Scalar(_))) {
                        // Scalar extends Scalar — stop here, don't follow extends chain
                        return current;
                    }
                    current = base_id;
                }
                _ => return current,
            }
        }
    }

    /// Check if a type is a template instance (has template_mapper set).
    /// Ported from TS checker.isTemplateInstance().
    pub fn is_template_instance(&self, type_id: TypeId) -> bool {
        match self.get_type(type_id) {
            Some(t) => type_utils::is_template_instance(t),
            None => false,
        }
    }

    /// Check for circular reference in type checking.
    /// Returns `Some(type_id)` if a circular reference is detected (caller should return early),
    /// or `None` if no circular reference was found.
    ///
    /// This replaces the common pattern at the start of check_* functions:
    /// ```ignore
    /// if self.pending_type_checks.contains(&node_id) {
    ///     if let Some(&type_id) = self.node_type_map.get(&node_id) {
    ///         return type_id;
    ///     }
    ///     return self.error_type;
    /// }
    /// ```
    pub(crate) fn check_circular_ref(&self, node_id: NodeId) -> Option<TypeId> {
        if self.pending_type_checks.contains(&node_id) {
            Some(
                self.node_type_map
                    .get(&node_id)
                    .copied()
                    .unwrap_or(self.error_type),
            )
        } else {
            None
        }
    }

    /// Finalize a type check by processing decorators, finishing template handling,
    /// and removing the node from the pending set.
    ///
    /// This replaces the common pattern at the end of check_* functions:
    /// ```ignore
    /// self.check_and_store_decorators(ctx, type_id, &node.decorators);
    /// self.finish_template_or_type(type_id, node_id, &node.template_parameters, &node.decorators, ctx.mapper.as_ref());
    /// self.pending_type_checks.remove(&node_id);
    /// ```
    pub(crate) fn finalize_type_check(
        &mut self,
        ctx: &CheckContext,
        type_id: TypeId,
        node_id: NodeId,
        template_params: &[NodeId],
        decorators: &[NodeId],
        mapper: Option<&TypeMapper>,
    ) {
        self.check_and_store_decorators(ctx, type_id, decorators);
        self.finish_template_or_type(type_id, node_id, template_params, decorators, mapper);
        self.pending_type_checks.remove(&node_id);
    }

    /// Infer scalar type from constraint for a value.
    pub fn infer_scalars_from_constraints(
        &mut self,
        value_id: ValueId,
        constraint_type: TypeId,
    ) -> TypeId {
        let value_type = self
            .get_value(value_id)
            .map(|v| v.value_type())
            .unwrap_or(self.error_type);

        if value_type == self.error_type {
            return self.error_type;
        }

        if let Some(Type::Scalar(s)) = self.get_type(constraint_type)
            && s.base_scalar.is_some()
        {
            return constraint_type;
        }

        value_type
    }

    /// Register a type by name in declared_types, update symbol links,
    /// and set template_node on the type if a mapper is provided.
    /// Ported from TS checker.ts registerType().
    ///
    /// For template instantiations (mapper is Some), only update instantiations
    /// cache — do NOT overwrite declared_types, declared_type, or
    /// is_template_instantiation, as those belong to the template declaration.
    pub(crate) fn register_type(
        &mut self,
        node_id: NodeId,
        type_id: TypeId,
        name: &str,
        mapper: Option<&TypeMapper>,
    ) {
        // Update symbol links
        let links = self.symbol_links.entry(node_id).or_default();

        // If we have a mapper, this is a template instantiation.
        // Only update the instantiations cache; don't overwrite declaration info.
        if let Some(mapper) = mapper {
            if !mapper.partial {
                // Track the instantiation for future reuse
                let instantiations = links.instantiations.get_or_insert_with(HashMap::new);
                instantiations.insert(mapper.args.clone(), type_id);
            }
            // Set type_id for the instantiation (used for cache lookups)
            links.type_id = Some(type_id);
            return;
        }

        // Template declaration (no mapper) — update full registration
        self.declared_types.insert(name.to_string(), type_id);
        links.declared_type = Some(type_id);
        links.type_id = Some(type_id);
    }

    /// Compute the template_node for a type declaration.
    /// If template_parameters is non-empty and there's no mapper (i.e., this is
    /// the template declaration, not an instantiation), set template_node to the
    /// declaration node so that template instances can reference it later.
    /// Ported from TS checker.ts computeTemplateNode().
    pub(crate) fn compute_template_node(
        &mut self,
        template_parameters: &[NodeId],
        mapper: Option<&TypeMapper>,
        node_id: NodeId,
    ) -> Option<NodeId> {
        if !template_parameters.is_empty() && mapper.is_none() {
            Some(node_id)
        } else {
            None
        }
    }
}

impl Default for Checker {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Standalone functions
// ============================================================================

/// Filter model properties, creating a new anonymous model if any properties are removed.
/// If no properties are filtered out, returns the original TypeId.
/// Ported from TS checker.ts filterModelProperties.
pub fn filter_model_properties(
    checker: &mut Checker,
    model_type_id: TypeId,
    filter: &dyn Fn(TypeId) -> bool,
) -> TypeId {
    let (props_to_keep, prop_names_to_keep) = {
        let mut keep_props: Vec<(String, TypeId)> = Vec::new();
        let mut keep_names: Vec<String> = Vec::new();
        if let Some(Type::Model(m)) = checker.get_type(model_type_id) {
            for name in &m.property_names {
                if let Some(&prop_id) = m.properties.get(name)
                    && filter(prop_id)
                {
                    keep_props.push((name.clone(), prop_id));
                    keep_names.push(name.clone());
                }
            }
        }
        (keep_props, keep_names)
    };

    // If no properties were filtered out, return original
    let original_count = match checker.get_type(model_type_id) {
        Some(Type::Model(m)) => m.properties.len(),
        _ => 0,
    };
    if props_to_keep.len() == original_count {
        return model_type_id;
    }

    // Create a new anonymous model with the filtered properties
    {
        let mut m = ModelType::new(checker.next_type_id(), String::new(), None, None);
        m.properties = props_to_keep.into_iter().collect();
        m.property_names = prop_names_to_keep;
        m.source_model = Some(model_type_id);
        m.is_finished = true;
        checker.create_type(Type::Model(m))
    }
}

// ============================================================================
// Test modules
// ============================================================================

#[cfg(test)]
mod alias_tests;
#[cfg(test)]
mod augment_decorator_tests;
#[cfg(test)]
mod check_parse_errors_tests;
#[cfg(test)]
mod circular_ref_tests;
#[cfg(test)]
mod clone_type_tests;
#[cfg(test)]
mod custom_decorator_tests;
#[cfg(test)]
mod decorators_tests;
#[cfg(test)]
mod deprecation_tests;
#[cfg(test)]
mod doc_comment_tests;
#[cfg(test)]
mod duplicate_ids_tests;
#[cfg(test)]
mod effective_type_tests;
#[cfg(test)]
mod enum_tests;
#[cfg(test)]
mod functions_tests;
#[cfg(test)]
mod global_ns_tests;
#[cfg(test)]
mod helpers_tests;
#[cfg(test)]
mod imports_tests;
#[cfg(test)]
mod integration_scenario_tests;
#[cfg(test)]
mod internal_tests;
#[cfg(test)]
mod intersection_tests;
#[cfg(test)]
mod model_tests;
#[cfg(test)]
mod namespace_tests;
#[cfg(test)]
mod operation_tests;
#[cfg(test)]
mod references_tests;
#[cfg(test)]
mod relation_tests;
#[cfg(test)]
mod resolve_type_reference_tests;
#[cfg(test)]
mod scalar_tests;
#[cfg(test)]
mod spread_tests;
#[cfg(test)]
mod string_template_tests;
#[cfg(test)]
mod template_tests;
#[cfg(test)]
pub(crate) mod test_utils;
#[cfg(test)]
mod tests;
#[cfg(test)]
mod type_utils_tests;
#[cfg(test)]
mod typeof_tests;
#[cfg(test)]
mod union_tests;
#[cfg(test)]
mod unused_template_parameter_tests;
#[cfg(test)]
mod unused_using_tests;
#[cfg(test)]
mod using_tests;
#[cfg(test)]
mod value_tests;
#[cfg(test)]
mod valueof_tests;