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
//! Type computation helpers, relationship queries, and format utilities.
//! This module extends `CheckerState` with additional methods for type-related
//! operations, providing cleaner APIs for common patterns.
use crate::diagnostics::Diagnostic;
use crate::query_boundaries::type_computation::evaluate_contextual_structure_with;
use crate::state::CheckerState;
use tsz_parser::parser::NodeIndex;
use tsz_parser::parser::syntax_kind_ext;
use tsz_scanner::SyntaxKind;
use tsz_solver::Visibility;
use tsz_solver::{ContextualTypeContext, TupleElement, TypeId, expression_ops};
// =============================================================================
// Type Computation Methods
// =============================================================================
impl<'a> CheckerState<'a> {
// =========================================================================
fn is_identifier_reference_to_global_nan(&self, node_idx: NodeIndex) -> bool {
let mut current_idx = node_idx;
while let Some(node) = self.ctx.arena.get(current_idx) {
if node.kind == tsz_parser::syntax_kind_ext::PARENTHESIZED_EXPRESSION
&& let Some(expr) = self.ctx.arena.get_parenthesized(node)
{
current_idx = expr.expression;
continue;
}
break;
}
if let Some(node) = self.ctx.arena.get(current_idx)
&& node.kind == tsz_scanner::SyntaxKind::Identifier as u16
&& let Some(ident) = self.ctx.arena.get_identifier(node)
&& ident.escaped_text == "NaN"
{
if let Some(sym_id) = self.resolve_identifier_symbol(current_idx) {
let is_global = self
.ctx
.binder
.get_symbol(sym_id)
.is_none_or(|s| s.parent.is_none());
return self.ctx.symbol_is_from_lib(sym_id) || is_global;
}
return true; // Unresolved NaN treated as global
}
false
}
// Core Type Computation
// =========================================================================
/// Evaluate a type deeply for binary operation checking.
///
/// Unlike `evaluate_type_with_resolution` which only handles the top-level type,
/// this also evaluates individual members of union types. This is needed because
/// types like `DeepPartial<number> | number` are stored as a union where one
/// member is an unevaluated Application type that the solver's `NumberLikeVisitor`
/// can't handle.
pub(crate) fn evaluate_type_for_binary_ops(&mut self, type_id: TypeId) -> TypeId {
let db = self.ctx.types;
let mut evaluate_leaf = |leaf_type: TypeId| self.evaluate_type_with_resolution(leaf_type);
evaluate_contextual_structure_with(db, type_id, &mut evaluate_leaf)
}
/// Evaluate a contextual type that may contain unevaluated mapped/conditional types.
///
/// When a generic function's parameter type is instantiated (e.g., `{ [K in keyof P]: P[K] }`
/// with P=Props), the result may be a mapped type with `Lazy` references that need a
/// full resolver to evaluate. The solver's default `contextual_property_type` uses
/// `NoopResolver` and can't resolve these. This method uses the Judge (which has access
/// to the `TypeEnvironment` resolver) to evaluate such types into concrete object types.
pub(crate) fn evaluate_contextual_type(&self, type_id: TypeId) -> TypeId {
let mut evaluate_leaf = |leaf_type: TypeId| self.judge_evaluate(leaf_type);
evaluate_contextual_structure_with(self.ctx.types, type_id, &mut evaluate_leaf)
}
/// Get the type of a conditional expression (ternary operator).
///
/// Computes the type of `condition ? whenTrue : whenFalse`.
/// Returns the union of the two branch types if they differ.
///
/// When a contextual type is available, each branch is checked against it
/// to catch type errors (TS2322).
///
/// Uses `solver::compute_conditional_expression_type` for type computation
/// as part of the Solver-First architecture migration.
pub(crate) fn get_type_of_conditional_expression(&mut self, idx: NodeIndex) -> TypeId {
let Some(node) = self.ctx.arena.get(idx) else {
return TypeId::ERROR;
};
let Some(cond) = self.ctx.arena.get_conditional_expr(node) else {
return TypeId::ERROR;
};
self.check_truthy_or_falsy(cond.condition);
// Get condition type for type computation
let condition_type = self.get_type_of_node(cond.condition);
// Apply contextual typing to each branch for better inference,
// but don't check assignability here - that happens at the call site.
// This allows `cond ? "a" : "b"` to infer as `"a" | "b"` and then
// the union is checked against the contextual type.
let prev_context = self.ctx.contextual_type;
// Preserve literal types in conditional branches so that
// `const x = cond ? "a" : "b"` infers `"a" | "b"` (tsc behavior).
let prev_preserve = self.ctx.preserve_literal_types;
self.ctx.preserve_literal_types = true;
// Compute branch types with the outer contextual type for inference.
// Branch typing may mutate contextual state while recursing, so restore
// it explicitly before each branch.
let when_true = self.get_type_of_node(cond.when_true);
self.ctx.contextual_type = prev_context;
let when_false = self.get_type_of_node(cond.when_false);
self.ctx.contextual_type = prev_context;
self.ctx.preserve_literal_types = prev_preserve;
// Use Solver API for type computation (Solver-First architecture)
expression_ops::compute_conditional_expression_type(
self.ctx.types,
condition_type,
when_true,
when_false,
)
}
/// Get type of array literal.
///
/// Computes the type of array literals like `[1, 2, 3]` or `["a", "b"]`.
/// Handles:
/// - Empty arrays (infer from context or use never[])
/// - Tuple contexts (e.g., `[string, number]`)
/// - Spread elements (`[...arr]`)
/// - Common type inference for mixed elements
pub(crate) fn get_type_of_array_literal(&mut self, idx: NodeIndex) -> TypeId {
let factory = self.ctx.types.factory();
let Some(node) = self.ctx.arena.get(idx) else {
return TypeId::ERROR;
};
let Some(array) = self.ctx.arena.get_literal_expr(node) else {
return TypeId::ERROR;
};
if array.elements.nodes.is_empty() {
// Empty array literal: infer from context or use never[]/any[]
// TypeScript uses "evolving array types" where [] starts as never[] and widens
// via control flow.
if let Some(contextual) = self.ctx.contextual_type {
let resolved = self.resolve_type_for_property_access(contextual);
let resolved = self.resolve_lazy_type(resolved);
if tsz_solver::type_queries::is_tuple_type(self.ctx.types, resolved) {
return factory.tuple(vec![]);
} else if let Some(t_elem) =
tsz_solver::type_queries::get_array_element_type(self.ctx.types, resolved)
{
return factory.array(t_elem);
}
}
// When noImplicitAny is off, empty array literals without contextual type
// are typed as any[] (matching tsc behavior). With noImplicitAny on, use never[]
// which is the "evolving array type" starting point.
if !self.ctx.no_implicit_any() {
return factory.array(TypeId::ANY);
}
return factory.array(TypeId::NEVER);
}
// Resolve lazy type aliases once and reuse for both tuple_context and ctx_helper
// This ensures type aliases (e.g., type Tup = [string, number]) are expanded
// before checking for tuple elements and providing contextual typing
let resolved_contextual_type = self
.ctx
.contextual_type
.map(|ctx_type| self.resolve_lazy_type(ctx_type));
// When the contextual type is a union like `[number] | string`, narrow it to
// only the array/tuple constituents applicable to an array literal. This ensures
// `[1]` with contextual type `[number] | string` is typed as `[number]` not `number[]`.
let applicable_contextual_type = resolved_contextual_type.and_then(|resolved| {
let evaluated = self.evaluate_application_type(resolved);
tsz_solver::type_queries::get_array_applicable_type(self.ctx.types, evaluated)
});
let tuple_context = match applicable_contextual_type {
Some(applicable) => {
tsz_solver::type_queries::get_tuple_elements(self.ctx.types, applicable)
}
None => None,
};
// Use the applicable (narrowed) type for contextual typing when available,
// falling back to the full resolved contextual type
let effective_contextual = applicable_contextual_type.or(resolved_contextual_type);
let ctx_helper = match effective_contextual {
Some(resolved) => Some(ContextualTypeContext::with_expected_and_options(
self.ctx.types,
resolved,
self.ctx.compiler_options.no_implicit_any,
)),
None => None,
};
// Get types of all elements, applying contextual typing when available.
// Track (type, node_index) pairs for excess property checking on array elements.
let mut element_types = Vec::new();
let mut element_nodes = Vec::new();
let mut tuple_elements = Vec::new();
for (index, &elem_idx) in array.elements.nodes.iter().enumerate() {
if elem_idx.is_none() {
continue;
}
let prev_context = self.ctx.contextual_type;
if let Some(ref helper) = ctx_helper {
if tuple_context.is_some() {
self.ctx.contextual_type = helper.get_tuple_element_type(index);
} else {
self.ctx.contextual_type = helper.get_array_element_type();
}
}
let Some(elem_node) = self.ctx.arena.get(elem_idx) else {
continue;
};
let elem_is_spread = elem_node.kind == syntax_kind_ext::SPREAD_ELEMENT;
// Handle spread elements - expand tuple types
if elem_is_spread && let Some(spread_data) = self.ctx.arena.get_spread(elem_node) {
let spread_expr_type = self.get_type_of_node(spread_data.expression);
let spread_expr_type = self.resolve_lazy_type(spread_expr_type);
// Check if spread argument is iterable, emit TS2488 if not.
// Skip this check when the array is a destructuring target
// (e.g., `[...c] = expr`), since the spread element is an assignment
// target, not a value being spread into a new array.
if !self.ctx.in_destructuring_target {
self.check_spread_iterability(spread_expr_type, spread_data.expression);
}
// If it's a tuple type, expand its elements
if let Some(elems) =
tsz_solver::type_queries::get_tuple_elements(self.ctx.types, spread_expr_type)
{
if let Some(ref _expected) = tuple_context {
// For tuple context, add each element with spread flag
for elem in &elems {
let (name, optional) =
match tuple_context.as_ref().and_then(|tc| tc.get(index)) {
Some(el) => (el.name, el.optional),
None => (None, false),
};
tuple_elements.push(TupleElement {
type_id: elem.type_id,
name,
optional,
rest: false, // Individual tuple elements are not spreads
});
// Don't increment index here - each tuple element maps to position
}
} else {
// For array context, add element types
for elem in &elems {
element_types.push(elem.type_id);
}
}
self.ctx.contextual_type = prev_context;
continue;
}
// For non-tuple spreads in array context, use element type
// For tuple context, use the spread type itself
let elem_type = if tuple_context.is_some() {
spread_expr_type
} else {
self.for_of_element_type(spread_expr_type)
};
self.ctx.contextual_type = prev_context;
if let Some(ref _expected) = tuple_context {
let (name, optional) = match tuple_context.as_ref().and_then(|tc| tc.get(index))
{
Some(el) => (el.name, el.optional),
None => (None, false),
};
tuple_elements.push(TupleElement {
type_id: elem_type,
name,
optional,
rest: true, // Mark as spread for non-tuple spreads in tuple context
});
} else {
element_types.push(elem_type);
}
continue;
}
// Regular (non-spread) element
let elem_type = self.get_type_of_node(elem_idx);
self.ctx.contextual_type = prev_context;
if let Some(ref _expected) = tuple_context {
let (name, optional) = match tuple_context.as_ref().and_then(|tc| tc.get(index)) {
Some(el) => (el.name, el.optional),
None => (None, false),
};
tuple_elements.push(TupleElement {
type_id: elem_type,
name,
optional,
rest: false,
});
} else {
element_types.push(elem_type);
element_nodes.push(elem_idx);
}
}
if tuple_context.is_some() {
return factory.tuple(tuple_elements);
}
// When in a const assertion context, array literals become tuples (not arrays)
// This allows [1, 2, 3] as const to become readonly [1, 2, 3] instead of readonly Array<number>
if self.ctx.in_const_assertion {
// Convert element_types to tuple_elements
let const_tuple_elements: Vec<tsz_solver::TupleElement> = element_types
.iter()
.map(|&type_id| tsz_solver::TupleElement {
type_id,
name: None,
optional: false,
rest: false,
})
.collect();
return factory.tuple(const_tuple_elements);
}
// Use contextual element type when available for better inference
if let Some(ref helper) = ctx_helper
&& let Some(context_element_type) = helper.get_array_element_type()
{
// Check if all elements are structurally compatible with the contextual type.
// IMPORTANT: Use is_subtype_of (structural check) instead of is_assignable_to
// because is_assignable_to includes excess property checking which would
// reject fresh object literals like `{a: 1, b: 2}` against `Foo {a: number}`.
// Excess properties should be checked separately, not block contextual typing.
if element_types
.iter()
.all(|&elem_type| self.is_subtype_of(elem_type, context_element_type))
{
// Check excess properties on each element before collapsing to contextual type.
// Fresh object literal types would be lost after returning Array<ContextualType>,
// so we must check excess properties here while the fresh types are still available.
for (elem_type, elem_node) in element_types.iter().zip(element_nodes.iter()) {
self.check_object_literal_excess_properties(
*elem_type,
context_element_type,
*elem_node,
);
}
return factory.array(context_element_type);
}
}
// Use Solver API for Best Common Type computation (Solver-First architecture)
let element_type = expression_ops::compute_best_common_type(
self.ctx.types,
&element_types,
Some(&self.ctx), // Pass TypeResolver for class hierarchy BCT
);
factory.array(element_type)
}
/// Get type of prefix unary expression.
///
/// Computes the type of unary expressions like `!x`, `+x`, `-x`, `~x`, `++x`, `--x`, `typeof x`.
/// Returns boolean for `!`, number for arithmetic operators, string for `typeof`.
pub(crate) fn get_type_of_prefix_unary(&mut self, idx: NodeIndex) -> TypeId {
use crate::diagnostics::{diagnostic_codes, diagnostic_messages, format_message};
use tsz_scanner::SyntaxKind;
use tsz_solver::type_queries::{LiteralTypeKind, classify_literal_type};
let Some(node) = self.ctx.arena.get(idx) else {
return TypeId::ERROR;
};
let Some(unary) = self.ctx.arena.get_unary_expr(node) else {
return TypeId::ERROR;
};
match unary.operator {
// ! returns boolean — also check operand for always-truthy/falsy (TS2872/TS2873)
k if k == SyntaxKind::ExclamationToken as u16 => {
// Type-check operand fully so inner expression diagnostics fire
// (e.g. TS18050 for `!(null + undefined)`).
self.get_type_of_node(unary.operand);
self.check_truthy_or_falsy(unary.operand);
TypeId::BOOLEAN
}
// typeof returns string but still type-check operand for flow/node types.
k if k == SyntaxKind::TypeOfKeyword as u16 => {
self.get_type_of_node(unary.operand);
TypeId::STRING
}
// Unary + and - return number unless contextual typing expects a numeric literal.
// Note: tsc does NOT validate operand types for unary +/-. Unary + is a
// common idiom for number conversion (+someString), and tsc allows it freely.
k if k == SyntaxKind::PlusToken as u16 || k == SyntaxKind::MinusToken as u16 => {
// Evaluate operand for side effects / flow analysis but don't type-check it
self.get_type_of_node(unary.operand);
if let Some(literal_type) = self.literal_type_from_initializer(idx) {
if self.contextual_literal_type(literal_type).is_some() {
return literal_type;
}
if matches!(
classify_literal_type(self.ctx.types, literal_type),
LiteralTypeKind::BigInt(_)
) {
if unary.operator == SyntaxKind::PlusToken as u16 {
if let Some(node) = self.ctx.arena.get(idx) {
let message = format_message(
diagnostic_messages::OPERATOR_CANNOT_BE_APPLIED_TO_TYPE,
&["+", "bigint"],
);
self.ctx.error(
node.pos,
node.end.saturating_sub(node.pos),
message,
diagnostic_codes::OPERATOR_CANNOT_BE_APPLIED_TO_TYPE,
);
}
return TypeId::ERROR;
}
// Preserve bigint literals for unary +/- to avoid widening to number in
// numeric-literal assignments (`const negZero: 0n = -0n`).
return literal_type;
}
}
TypeId::NUMBER
}
// ~ (bitwise NOT) returns number
// Note: tsc does NOT validate operand types for ~, same as unary +/-
k if k == SyntaxKind::TildeToken as u16 => {
// Evaluate operand for side effects / flow analysis but don't type-check it
self.get_type_of_node(unary.operand);
TypeId::NUMBER
}
// ++ and -- require numeric operand and valid l-value
k if k == SyntaxKind::PlusPlusToken as u16
|| k == SyntaxKind::MinusMinusToken as u16 =>
{
// TS1100: Invalid use of 'eval'/'arguments' in strict mode.
// Must come before TS2356 to match TSC's diagnostic priority.
let mut emitted_strict = false;
if let Some(operand_node) = self.ctx.arena.get(unary.operand)
&& operand_node.kind == SyntaxKind::Identifier as u16
&& let Some(id_data) = self.ctx.arena.get_identifier(operand_node)
&& (id_data.escaped_text == "eval" || id_data.escaped_text == "arguments")
&& self.is_strict_mode_for_node(unary.operand)
{
use crate::diagnostics::diagnostic_codes;
let code = if self.ctx.enclosing_class.is_some() {
diagnostic_codes::CODE_CONTAINED_IN_A_CLASS_IS_EVALUATED_IN_JAVASCRIPTS_STRICT_MODE_WHICH_DOES_NOT
} else {
diagnostic_codes::INVALID_USE_OF_IN_STRICT_MODE
};
self.error_at_node_msg(unary.operand, code, &[&id_data.escaped_text]);
emitted_strict = true;
}
// Get operand type for validation.
// TSC checks arithmetic type BEFORE lvalue — if the type check
// fails (TS2356), the lvalue check (TS2357) is skipped.
let operand_type = self.get_type_of_node(unary.operand);
let mut arithmetic_ok = true;
if !emitted_strict {
use tsz_solver::BinaryOpEvaluator;
let evaluator = BinaryOpEvaluator::new(self.ctx.types);
// When strictNullChecks is off, null/undefined are silently
// assignable to number, so skip arithmetic check for them.
let is_valid = evaluator.is_arithmetic_operand(operand_type)
|| (!self.ctx.strict_null_checks()
&& (operand_type == TypeId::NULL || operand_type == TypeId::UNDEFINED));
if !is_valid {
arithmetic_ok = false;
// Emit TS2356 for invalid increment/decrement operand type
if let Some(loc) = self.get_source_location(unary.operand) {
use crate::diagnostics::{diagnostic_codes, diagnostic_messages};
self.ctx.diagnostics.push(Diagnostic::error(self.ctx.file_name.clone(), loc.start, loc.length(), diagnostic_messages::AN_ARITHMETIC_OPERAND_MUST_BE_OF_TYPE_ANY_NUMBER_BIGINT_OR_AN_ENUM_TYPE.to_string(), diagnostic_codes::AN_ARITHMETIC_OPERAND_MUST_BE_OF_TYPE_ANY_NUMBER_BIGINT_OR_AN_ENUM_TYPE));
}
}
}
// Only check lvalue and assignment restrictions when arithmetic
// type is valid (matches TSC: TS2357 is skipped when TS2356 fires).
if arithmetic_ok {
let emitted_lvalue = self.check_increment_decrement_operand(unary.operand);
if !emitted_lvalue {
// TS2588: Cannot assign to 'x' because it is a constant.
let is_const = self.check_const_assignment(unary.operand);
// TS2630: Cannot assign to 'x' because it is a function.
self.check_function_assignment(unary.operand);
// TS2540: Cannot assign to readonly property
if !is_const {
self.check_readonly_assignment(unary.operand, idx);
}
}
}
TypeId::NUMBER
}
// delete returns boolean and checks that operand is a property reference
k if k == SyntaxKind::DeleteKeyword as u16 => {
// Evaluate operand for side effects / flow analysis
self.get_type_of_node(unary.operand);
// TS1102: delete cannot be called on an identifier in strict mode.
let is_identifier_operand = unary.operand.is_some()
&& self
.ctx
.arena
.get(unary.operand)
.is_some_and(|operand_node| {
operand_node.kind == SyntaxKind::Identifier as u16
});
if is_identifier_operand && self.is_strict_mode_for_node(idx) {
self.error_at_node(
idx,
crate::diagnostics::diagnostic_messages::DELETE_CANNOT_BE_CALLED_ON_AN_IDENTIFIER_IN_STRICT_MODE,
crate::diagnostics::diagnostic_codes::DELETE_CANNOT_BE_CALLED_ON_AN_IDENTIFIER_IN_STRICT_MODE,
);
}
// TS2703: The operand of a 'delete' operator must be a property reference.
// Valid operands: property access (obj.prop), element access (obj["prop"]),
// or optional chain (obj?.prop). All other expressions are invalid.
let is_property_reference = unary.operand.is_some()
&& self
.ctx
.arena
.get(unary.operand)
.is_some_and(|operand_node| {
use tsz_parser::parser::syntax_kind_ext;
operand_node.kind == syntax_kind_ext::PROPERTY_ACCESS_EXPRESSION
|| operand_node.kind == syntax_kind_ext::ELEMENT_ACCESS_EXPRESSION
});
if !is_property_reference {
self.error_at_node(
idx,
crate::diagnostics::diagnostic_messages::THE_OPERAND_OF_A_DELETE_OPERATOR_MUST_BE_A_PROPERTY_REFERENCE,
crate::diagnostics::diagnostic_codes::THE_OPERAND_OF_A_DELETE_OPERATOR_MUST_BE_A_PROPERTY_REFERENCE,
);
}
// TS2790: In strictNullChecks, delete is only allowed for optional properties.
// With exactOptionalPropertyTypes disabled, properties whose declared type
// includes `undefined` are also treated as deletable.
if self.ctx.compiler_options.strict_null_checks
&& let Some(operand_node) = self.ctx.arena.get(unary.operand)
&& operand_node.kind == syntax_kind_ext::PROPERTY_ACCESS_EXPRESSION
&& let Some(access) = self.ctx.arena.get_access_expr(operand_node)
{
use tsz_solver::operations_property::PropertyAccessResult;
let prop_name = self
.ctx
.arena
.get_identifier_at(access.name_or_argument)
.map(|ident| ident.escaped_text.clone())
.or_else(|| self.get_literal_string_from_node(access.name_or_argument));
if let Some(prop_name) = prop_name {
let object_type = self.get_type_of_node(access.expression);
if object_type != TypeId::ANY
&& object_type != TypeId::UNKNOWN
&& object_type != TypeId::ERROR
&& object_type != TypeId::NEVER
&& let PropertyAccessResult::Success { type_id, .. } =
self.resolve_property_access_with_env(object_type, &prop_name)
{
let is_optional = self.is_property_optional(object_type, &prop_name);
let optional_via_undefined =
!self.ctx.compiler_options.exact_optional_property_types
&& tsz_solver::type_queries::type_includes_undefined(
self.ctx.types,
type_id,
);
if !is_optional && !optional_via_undefined {
self.error_at_node(
idx,
crate::diagnostics::diagnostic_messages::THE_OPERAND_OF_A_DELETE_OPERATOR_MUST_BE_OPTIONAL,
crate::diagnostics::diagnostic_codes::THE_OPERAND_OF_A_DELETE_OPERATOR_MUST_BE_OPTIONAL,
);
}
}
}
}
TypeId::BOOLEAN
}
// void returns undefined
k if k == SyntaxKind::VoidKeyword as u16 => {
// Evaluate operand for side effects / flow analysis
self.get_type_of_node(unary.operand);
TypeId::UNDEFINED
}
_ => TypeId::ANY,
}
}
pub(crate) fn is_strict_mode_for_node(&self, idx: NodeIndex) -> bool {
if self.ctx.compiler_options.always_strict {
return true;
}
let is_external_module = self
.ctx
.is_external_module_by_file
.as_ref()
.is_some_and(|map| map.get(&self.ctx.file_name).is_some_and(|is_ext| *is_ext))
|| self.ctx.binder.is_external_module();
if is_external_module {
return true;
}
let statement_is_use_strict = |stmt_idx: NodeIndex, ctx: &Self| -> bool {
ctx.ctx
.arena
.get(stmt_idx)
.filter(|stmt| stmt.kind == syntax_kind_ext::EXPRESSION_STATEMENT)
.and_then(|stmt| ctx.ctx.arena.get_expression_statement(stmt))
.and_then(|expr_stmt| ctx.ctx.arena.get(expr_stmt.expression))
.filter(|expr_node| expr_node.kind == SyntaxKind::StringLiteral as u16)
.and_then(|expr_node| ctx.ctx.arena.get_literal(expr_node))
.is_some_and(|lit| lit.text == "use strict")
};
let block_has_use_strict = |block_idx: NodeIndex, ctx: &Self| -> bool {
let Some(block_node) = ctx.ctx.arena.get(block_idx) else {
return false;
};
let Some(block) = ctx.ctx.arena.get_block(block_node) else {
return false;
};
for &stmt_idx in &block.statements.nodes {
if statement_is_use_strict(stmt_idx, ctx) {
return true;
}
let Some(stmt_node) = ctx.ctx.arena.get(stmt_idx) else {
return false;
};
if stmt_node.kind != syntax_kind_ext::EXPRESSION_STATEMENT {
break;
}
}
false
};
let mut current = idx;
loop {
let Some(node) = self.ctx.arena.get(current) else {
return false;
};
if node.kind == syntax_kind_ext::CLASS_DECLARATION
|| node.kind == syntax_kind_ext::CLASS_EXPRESSION
{
return true;
}
let Some(ext) = self.ctx.arena.get_extended(current) else {
return false;
};
let parent = ext.parent;
if parent.is_none() {
return false;
}
let Some(parent_node) = self.ctx.arena.get(parent) else {
return false;
};
match parent_node.kind {
k if k == syntax_kind_ext::SOURCE_FILE => {
if let Some(sf) = self.ctx.arena.get_source_file(parent_node)
&& sf
.statements
.nodes
.iter()
.any(|stmt_idx| statement_is_use_strict(*stmt_idx, self))
{
return true;
}
return false;
}
k if k == syntax_kind_ext::FUNCTION_DECLARATION
|| k == syntax_kind_ext::FUNCTION_EXPRESSION
|| k == syntax_kind_ext::ARROW_FUNCTION =>
{
if let Some(func) = self.ctx.arena.get_function(parent_node)
&& func.body.is_some()
&& block_has_use_strict(func.body, self)
{
return true;
}
}
_ => {}
}
current = parent;
}
}
/// Get type of template expression (template literal with substitutions).
///
/// Type-checks all expressions within template spans to emit errors like TS2304.
/// Template expressions always produce string type.
///
/// Uses `solver::compute_template_expression_type` for type computation
/// as part of the Solver-First architecture migration.
pub(crate) fn get_type_of_template_expression(&mut self, idx: NodeIndex) -> TypeId {
let Some(node) = self.ctx.arena.get(idx) else {
return TypeId::STRING;
};
let Some(template) = self.ctx.arena.get_template_expr(node) else {
return TypeId::STRING;
};
// Type-check each template span's expression and collect types for solver
let mut part_types = Vec::new();
for &span_idx in &template.template_spans.nodes {
let Some(span_node) = self.ctx.arena.get(span_idx) else {
continue;
};
let Some(span) = self.ctx.arena.get_template_span(span_node) else {
continue;
};
// Type-check the expression - this will emit TS2304 if name is unresolved
let part_type = self.get_type_of_node(span.expression);
part_types.push(part_type);
}
// Use Solver API for type computation (Solver-First architecture)
// Template literals always produce string type, but we check for ERROR/NEVER propagation
expression_ops::compute_template_expression_type(self.ctx.types, &part_types)
}
/// Get type of variable declaration.
///
/// Computes the type of variable declarations like `let x: number = 5` or `const y = "hello"`.
/// Returns the type annotation if present, otherwise infers from the initializer.
pub(crate) fn get_type_of_variable_declaration(&mut self, idx: NodeIndex) -> TypeId {
let Some(node) = self.ctx.arena.get(idx) else {
return TypeId::ERROR;
};
let Some(var_decl) = self.ctx.arena.get_variable_declaration(node) else {
return TypeId::ERROR;
};
// First check type annotation - this takes precedence
if var_decl.type_annotation.is_some() {
return self.get_type_from_type_node(var_decl.type_annotation);
}
if self.is_catch_clause_variable_declaration(idx)
&& self.ctx.use_unknown_in_catch_variables()
{
return TypeId::UNKNOWN;
}
// Infer from initializer
if var_decl.initializer.is_some() {
let init_type = self.get_type_of_node(var_decl.initializer);
// Rule #10: Literal Widening (with freshness)
// For mutable bindings (let/var), widen literals to their primitive type
// ONLY when the initializer is a "fresh" literal expression (direct literal
// in source code). Types from variable references, narrowing, or computed
// expressions are "non-fresh" and should NOT be widened.
// For const bindings, preserve literal types (unless in array/object context)
if !self.is_const_variable_declaration(idx) {
let widened = if self.is_fresh_literal_expression(var_decl.initializer) {
self.widen_initializer_type_for_mutable_binding(init_type)
} else {
init_type
};
// When strictNullChecks is off, undefined and null widen to any
// (always, regardless of freshness)
if !self.ctx.strict_null_checks()
&& tsz_solver::type_queries::is_only_null_or_undefined(self.ctx.types, widened)
{
return TypeId::ANY;
}
return widened;
}
// const: preserve literal type
init_type
} else {
// No initializer - use UNKNOWN to enforce strict checking
// This requires explicit type annotation or prevents unsafe usage
TypeId::UNKNOWN
}
}
/// Get the type of an assignment target without definite assignment checks.
///
/// Computes the type of the left-hand side of an assignment expression.
/// Handles identifier resolution and type-only alias checking.
pub(crate) fn get_type_of_assignment_target(&mut self, idx: NodeIndex) -> TypeId {
use tsz_scanner::SyntaxKind;
if let Some(node) = self.ctx.arena.get(idx)
&& node.kind == SyntaxKind::Identifier as u16
{
// Check for local variable first (including "arguments" shadowing).
// This handles: `const arguments = ...; arguments = foo;`
if let Some(sym_id) = self.resolve_identifier_symbol_for_write(idx) {
if self.alias_resolves_to_type_only(sym_id) {
if let Some(ident) = self.ctx.arena.get_identifier(node) {
self.error_type_only_value_at(&ident.escaped_text, idx);
}
return TypeId::ERROR;
}
// Check if this is "arguments" in a function body with a local declaration
if let Some(ident) = self.ctx.arena.get_identifier(node) {
if ident.escaped_text == "arguments" && self.is_in_regular_function_body(idx) {
// Check if the declaration is local to the current function
if let Some(symbol) = self.ctx.binder.get_symbol(sym_id)
&& !symbol.declarations.is_empty()
{
let decl_node = symbol.declarations[0];
if let Some(current_fn) = self.find_enclosing_function(idx)
&& let Some(decl_fn) = self.find_enclosing_function(decl_node)
&& current_fn == decl_fn
{
// Local "arguments" declaration - use it
let declared_type = self.get_type_of_symbol(sym_id);
return declared_type;
}
}
// Symbol found but not local - fall through to IArguments check below
} else {
// Not "arguments" or not in function - use the symbol
let declared_type = self.get_type_of_symbol(sym_id);
return declared_type;
}
} else {
// Use the resolved symbol
let declared_type = self.get_type_of_symbol(sym_id);
return declared_type;
}
}
// Inside a regular function body, `arguments` is the implicit IArguments object,
// overriding any outer `arguments` declaration (but not local ones, checked above).
if let Some(ident) = self.ctx.arena.get_identifier(node)
&& ident.escaped_text == "arguments"
&& self.is_in_regular_function_body(idx)
{
let lib_binders = self.get_lib_binders();
if let Some(sym_id) = self
.ctx
.binder
.get_global_type_with_libs("IArguments", &lib_binders)
{
return self.type_reference_symbol_type(sym_id);
}
return TypeId::ANY;
}
}
// Instantiation expressions on the left side (e.g. `fn<T> = ...`) are invalid (TS2364),
// but the base expression is still a value read and must participate in
// definite assignment checks (TS2454).
if let Some(node) = self.ctx.arena.get(idx)
&& node.kind == syntax_kind_ext::EXPRESSION_WITH_TYPE_ARGUMENTS
&& let Some(expr_type_args) = self.ctx.arena.get_expr_type_args(node)
&& expr_type_args
.type_arguments
.as_ref()
.is_some_and(|args| !args.nodes.is_empty())
{
let base_expr = expr_type_args.expression;
let _ = self.get_type_of_node(base_expr);
// In assignment-target context, flow nodes may attach to the outer
// instantiation expression rather than the inner identifier. Force
// definite-assignment checking for `id<T> = ...` to match tsc.
if let Some(base_node) = self.ctx.arena.get(base_expr)
&& base_node.kind == SyntaxKind::Identifier as u16
&& let Some(sym_id) = self.resolve_identifier_symbol(base_expr)
{
let declared_type = self.get_type_of_symbol(sym_id);
let _ = self.check_flow_usage(base_expr, declared_type, sym_id);
}
}
// For non-identifier assignment targets (property access, element access, etc.),
// we need the declared type without control-flow narrowing.
// Example: After `if (foo[x] === undefined)`, when checking `foo[x] = 1`,
// we should check against the declared type (e.g., `number | undefined` from index signature)
// not the narrowed type (e.g., `undefined`).
//
// However, if the target is invalid (e.g. `getValue<number> = ...` parsed as BinaryExpression),
// we should NOT skip narrowing because we want to treat it as an expression read
// to catch errors like TS2454 (used before assigned).
let prev_skip_narrowing = self.ctx.skip_flow_narrowing;
if self.is_valid_assignment_target(idx) {
self.ctx.skip_flow_narrowing = true;
}
let result = self.get_type_of_node(idx);
self.ctx.skip_flow_narrowing = prev_skip_narrowing;
result
}
/// Get the type of a class member.
///
/// Computes the type for class property declarations, method declarations, and getters.
pub(crate) fn get_type_of_class_member(&mut self, member_idx: NodeIndex) -> TypeId {
use tsz_parser::parser::syntax_kind_ext;
let Some(member_node) = self.ctx.arena.get(member_idx) else {
return TypeId::ANY;
};
match member_node.kind {
k if k == syntax_kind_ext::PROPERTY_DECLARATION => {
let Some(prop) = self.ctx.arena.get_property_decl(member_node) else {
return TypeId::ANY;
};
// Get the type: either from annotation or inferred from initializer
if prop.type_annotation.is_some() {
self.get_type_from_type_node(prop.type_annotation)
} else if prop.initializer.is_some() {
self.get_type_of_node(prop.initializer)
} else {
TypeId::ANY
}
}
k if k == syntax_kind_ext::METHOD_DECLARATION => {
let Some(method) = self.ctx.arena.get_method_decl(member_node) else {
return TypeId::ANY;
};
let signature = self.call_signature_from_method(method, member_idx);
use tsz_solver::FunctionShape;
let factory = self.ctx.types.factory();
factory.function(FunctionShape {
type_params: signature.type_params,
params: signature.params,
this_type: signature.this_type,
return_type: signature.return_type,
type_predicate: signature.type_predicate,
is_constructor: false,
is_method: true,
})
}
k if k == syntax_kind_ext::GET_ACCESSOR => {
let Some(accessor) = self.ctx.arena.get_accessor(member_node) else {
return TypeId::ANY;
};
if accessor.type_annotation.is_some() {
self.get_type_from_type_node(accessor.type_annotation)
} else {
self.infer_getter_return_type(accessor.body)
}
}
_ => TypeId::ANY,
}
}
/// Get the simple type of an interface member (without wrapping in object type).
///
/// For property signatures: returns the property type
/// For method signatures: returns the function type
pub(crate) fn get_type_of_interface_member_simple(&mut self, member_idx: NodeIndex) -> TypeId {
use tsz_parser::parser::syntax_kind_ext::{METHOD_SIGNATURE, PROPERTY_SIGNATURE};
use tsz_solver::FunctionShape;
let factory = self.ctx.types.factory();
let Some(member_node) = self.ctx.arena.get(member_idx) else {
return TypeId::ANY;
};
if member_node.kind == METHOD_SIGNATURE {
let Some(sig) = self.ctx.arena.get_signature(member_node) else {
return TypeId::ANY;
};
let (type_params, type_param_updates) = self.push_type_parameters(&sig.type_parameters);
let (params, this_type) = self.extract_params_from_signature(sig);
let (return_type, type_predicate) =
self.return_type_and_predicate(sig.type_annotation, ¶ms);
let shape = FunctionShape {
type_params,
params,
this_type,
return_type,
type_predicate,
is_constructor: false,
is_method: true,
};
self.pop_type_parameters(type_param_updates);
return factory.function(shape);
}
if member_node.kind == PROPERTY_SIGNATURE {
let Some(sig) = self.ctx.arena.get_signature(member_node) else {
return TypeId::ANY;
};
if sig.type_annotation.is_some() {
return self.get_type_from_type_node(sig.type_annotation);
}
return TypeId::ANY;
}
TypeId::ANY
}
/// Get the type of an interface member.
///
/// Returns an object type containing the member. For method signatures,
/// creates a callable type. For property signatures, creates a property type.
pub(crate) fn get_type_of_interface_member(&mut self, member_idx: NodeIndex) -> TypeId {
use tsz_parser::parser::syntax_kind_ext::{METHOD_SIGNATURE, PROPERTY_SIGNATURE};
use tsz_solver::{FunctionShape, PropertyInfo};
let factory = self.ctx.types.factory();
let Some(member_node) = self.ctx.arena.get(member_idx) else {
return TypeId::ERROR;
};
if member_node.kind == METHOD_SIGNATURE || member_node.kind == PROPERTY_SIGNATURE {
let Some(sig) = self.ctx.arena.get_signature(member_node) else {
return TypeId::ERROR;
};
let name = self.get_property_name(sig.name);
let Some(name) = name else {
return TypeId::ERROR;
};
let name_atom = self.ctx.types.intern_string(&name);
if member_node.kind == METHOD_SIGNATURE {
let (type_params, type_param_updates) =
self.push_type_parameters(&sig.type_parameters);
let (params, this_type) = self.extract_params_from_signature(sig);
let (return_type, type_predicate) =
self.return_type_and_predicate(sig.type_annotation, ¶ms);
let shape = FunctionShape {
type_params,
params,
this_type,
return_type,
type_predicate,
is_constructor: false,
is_method: true,
};
self.pop_type_parameters(type_param_updates);
let method_type = factory.function(shape);
let prop = PropertyInfo {
name: name_atom,
type_id: method_type,
write_type: method_type,
optional: sig.question_token,
readonly: self.has_readonly_modifier(&sig.modifiers),
is_method: true,
visibility: Visibility::Public,
parent_id: None,
};
return factory.object(vec![prop]);
}
let type_id = if sig.type_annotation.is_some() {
self.get_type_from_type_node(sig.type_annotation)
} else {
TypeId::ANY
};
let prop = PropertyInfo {
name: name_atom,
type_id,
write_type: type_id,
optional: sig.question_token,
readonly: self.has_readonly_modifier(&sig.modifiers),
is_method: false,
visibility: Visibility::Public,
parent_id: None,
};
return factory.object(vec![prop]);
}
TypeId::ANY
}
/// Get the type of a binary expression.
///
/// Handles all binary operators including arithmetic, comparison, logical,
/// assignment, nullish coalescing, and comma operators.
pub(crate) fn get_type_of_binary_expression(&mut self, idx: NodeIndex) -> TypeId {
use tsz_scanner::SyntaxKind;
use tsz_solver::{BinaryOpEvaluator, BinaryOpResult};
let factory = self.ctx.types.factory();
let evaluator = BinaryOpEvaluator::new(self.ctx.types);
let mut stack = vec![(idx, false)];
let mut type_stack: Vec<TypeId> = Vec::new();
while let Some((node_idx, visited)) = stack.pop() {
let Some(node) = self.ctx.arena.get(node_idx) else {
// Return UNKNOWN instead of ANY when node cannot be found
type_stack.push(TypeId::UNKNOWN);
continue;
};
if node.kind != syntax_kind_ext::BINARY_EXPRESSION {
type_stack.push(self.get_type_of_node(node_idx));
continue;
}
let Some(binary) = self.ctx.arena.get_binary_expr(node) else {
// Return UNKNOWN instead of ANY when binary expression cannot be extracted
type_stack.push(TypeId::UNKNOWN);
continue;
};
let left_idx = binary.left;
let right_idx = binary.right;
let op_kind = binary.operator_token;
// TS5076: Check for mixing ?? with || or && without parentheses.
// Only check on first visit to avoid duplicates from the stack-based iteration.
if !visited {
let is_nullish_coalescing = op_kind == SyntaxKind::QuestionQuestionToken as u16;
let is_logical = op_kind == SyntaxKind::BarBarToken as u16
|| op_kind == SyntaxKind::AmpersandAmpersandToken as u16;
if is_nullish_coalescing || is_logical {
// Check left operand: is it a binary expr with a conflicting operator?
if let Some(left_node) = self.ctx.arena.get(left_idx)
&& left_node.kind == syntax_kind_ext::BINARY_EXPRESSION
&& let Some(left_binary) = self.ctx.arena.get_binary_expr(left_node)
{
let left_op = left_binary.operator_token;
let left_is_nullish = left_op == SyntaxKind::QuestionQuestionToken as u16;
let left_is_logical = left_op == SyntaxKind::BarBarToken as u16
|| left_op == SyntaxKind::AmpersandAmpersandToken as u16;
if (is_nullish_coalescing && left_is_logical)
|| (is_logical && left_is_nullish)
{
// Determine operator names for the error message
let left_op_str = if left_is_nullish {
"??"
} else if left_op == SyntaxKind::BarBarToken as u16 {
"||"
} else {
"&&"
};
let right_op_str = if is_nullish_coalescing {
"??"
} else if op_kind == SyntaxKind::BarBarToken as u16 {
"||"
} else {
"&&"
};
if let Some(loc) = self.get_source_location(left_idx) {
use crate::diagnostics::{
Diagnostic, diagnostic_codes, diagnostic_messages,
format_message,
};
self.ctx.diagnostics.push(Diagnostic::error(self.ctx.file_name.clone(), loc.start, loc.length(), format_message(diagnostic_messages::AND_OPERATIONS_CANNOT_BE_MIXED_WITHOUT_PARENTHESES, &[left_op_str, right_op_str]), diagnostic_codes::AND_OPERATIONS_CANNOT_BE_MIXED_WITHOUT_PARENTHESES));
}
}
}
// Check right operand
if let Some(right_node) = self.ctx.arena.get(right_idx)
&& right_node.kind == syntax_kind_ext::BINARY_EXPRESSION
&& let Some(right_binary) = self.ctx.arena.get_binary_expr(right_node)
{
let right_op = right_binary.operator_token;
let right_is_nullish = right_op == SyntaxKind::QuestionQuestionToken as u16;
let right_is_logical = right_op == SyntaxKind::BarBarToken as u16
|| right_op == SyntaxKind::AmpersandAmpersandToken as u16;
if (is_nullish_coalescing && right_is_logical)
|| (is_logical && right_is_nullish)
{
let outer_op_str = if is_nullish_coalescing {
"??"
} else if op_kind == SyntaxKind::BarBarToken as u16 {
"||"
} else {
"&&"
};
let inner_op_str = if right_is_nullish {
"??"
} else if right_op == SyntaxKind::BarBarToken as u16 {
"||"
} else {
"&&"
};
if let Some(loc) = self.get_source_location(right_idx) {
use crate::diagnostics::{
Diagnostic, diagnostic_codes, diagnostic_messages,
format_message,
};
self.ctx.diagnostics.push(Diagnostic::error(self.ctx.file_name.clone(), loc.start, loc.length(), format_message(diagnostic_messages::AND_OPERATIONS_CANNOT_BE_MIXED_WITHOUT_PARENTHESES, &[inner_op_str, outer_op_str]), diagnostic_codes::AND_OPERATIONS_CANNOT_BE_MIXED_WITHOUT_PARENTHESES));
}
}
}
}
}
if !visited {
if self.is_assignment_operator(op_kind) {
let assign_type = if op_kind == SyntaxKind::EqualsToken as u16 {
self.check_assignment_expression(left_idx, right_idx, node_idx)
} else {
self.check_compound_assignment_expression(
left_idx, right_idx, op_kind, node_idx,
)
};
type_stack.push(assign_type);
continue;
}
// For &&, the right operand gets the contextual type of the whole
// expression (inherited from parent, e.g. assignment target).
// For || and ??, the right operand gets the outer contextual type
// if available, falling back to the left type (minus nullish).
// This enables contextual typing of callbacks:
// let x: (a: string) => string;
// x = y && (a => a); // a: string from assignment context
// let g = f || (x => { ... }); // x: string from left type fallback
if op_kind == SyntaxKind::AmpersandAmpersandToken as u16 {
// && passes outer contextual type to the right operand only.
// The left operand gets no contextual type.
let prev_context = self.ctx.contextual_type;
self.ctx.contextual_type = None;
let left_type = self.get_type_of_node(left_idx);
self.ctx.contextual_type = prev_context;
let right_type = self.get_type_of_node(right_idx);
type_stack.push(left_type);
type_stack.push(right_type);
stack.push((node_idx, true));
continue;
}
if op_kind == SyntaxKind::BarBarToken as u16
|| op_kind == SyntaxKind::QuestionQuestionToken as u16
{
let left_type = self.get_type_of_node(left_idx);
// Right operand: use left type (minus nullish) as contextual type
let prev_context = self.ctx.contextual_type;
let non_nullish = self.ctx.types.remove_nullish(left_type);
if non_nullish != TypeId::NEVER && non_nullish != TypeId::UNKNOWN {
self.ctx.contextual_type = Some(non_nullish);
}
let right_type = self.get_type_of_node(right_idx);
self.ctx.contextual_type = prev_context;
type_stack.push(left_type);
type_stack.push(right_type);
stack.push((node_idx, true));
continue;
}
// For comma operator: left gets no contextual type,
// right gets the outer contextual type
if op_kind == SyntaxKind::CommaToken as u16 {
let prev_context = self.ctx.contextual_type;
self.ctx.contextual_type = None;
let left_type = self.get_type_of_node(left_idx);
self.ctx.contextual_type = prev_context;
let right_type = self.get_type_of_node(right_idx);
type_stack.push(left_type);
type_stack.push(right_type);
stack.push((node_idx, true));
continue;
}
stack.push((node_idx, true));
stack.push((right_idx, false));
stack.push((left_idx, false));
continue;
}
// Return UNKNOWN instead of ANY when type_stack is empty
let right_type = type_stack.pop().unwrap_or(TypeId::UNKNOWN);
let left_type = type_stack.pop().unwrap_or(TypeId::UNKNOWN);
if op_kind == SyntaxKind::CommaToken as u16 {
// TS2695: Emit when left side has no side effects
// TypeScript suppresses this diagnostic when allowUnreachableCode is enabled
// TypeScript DOES emit this even when left operand has type errors or is typed as any
if self.ctx.compiler_options.allow_unreachable_code != Some(true)
&& self.is_side_effect_free(left_idx)
&& !self.is_indirect_call(node_idx, left_idx, right_idx)
{
use crate::diagnostics::{diagnostic_codes, diagnostic_messages};
self.error_at_node(
left_idx,
diagnostic_messages::LEFT_SIDE_OF_COMMA_OPERATOR_IS_UNUSED_AND_HAS_NO_SIDE_EFFECTS,
diagnostic_codes::LEFT_SIDE_OF_COMMA_OPERATOR_IS_UNUSED_AND_HAS_NO_SIDE_EFFECTS,
);
}
type_stack.push(right_type);
continue;
}
if op_kind == SyntaxKind::InKeyword as u16 {
if let Some(left_node) = self.ctx.arena.get(left_idx)
&& left_node.kind == SyntaxKind::PrivateIdentifier as u16
{
self.check_private_identifier_in_expression(left_idx, right_type);
}
// TS2322: The right-hand side of an 'in' expression must be assignable to 'object'
// This prevents using 'in' with primitives like string | number
if right_type != TypeId::ANY && right_type != TypeId::ERROR {
let _ = self.check_assignable_or_report(right_type, TypeId::OBJECT, right_idx);
}
type_stack.push(TypeId::BOOLEAN);
continue;
}
// instanceof always produces boolean
if op_kind == SyntaxKind::InstanceOfKeyword as u16 {
use crate::diagnostics::{diagnostic_codes, diagnostic_messages, format_message};
let eval_left = self.evaluate_type_for_assignability(left_type);
if eval_left != TypeId::ERROR {
let evaluator = BinaryOpEvaluator::new(self.ctx.types);
if !evaluator.is_valid_instanceof_left_operand(eval_left)
&& let Some(left_node) = self.ctx.arena.get(left_idx)
{
let message = format_message(
diagnostic_messages::THE_LEFT_HAND_SIDE_OF_AN_INSTANCEOF_EXPRESSION_MUST_BE_OF_TYPE_ANY_AN_OBJECT_TYP,
&[],
);
self.ctx.diagnostics.push(Diagnostic::error(
self.ctx.file_name.clone(),
left_node.pos,
left_node.end - left_node.pos,
message,
diagnostic_codes::THE_LEFT_HAND_SIDE_OF_AN_INSTANCEOF_EXPRESSION_MUST_BE_OF_TYPE_ANY_AN_OBJECT_TYP,
));
}
}
let eval_right = self.evaluate_type_for_assignability(right_type);
if eval_right != TypeId::ERROR {
let mut is_valid_rhs = false;
let func_ty_opt = self
.ctx
.binder
.file_locals
.get("Function")
.map(|sym_id| self.get_type_of_symbol(sym_id))
.or_else(|| self.resolve_lib_type_by_name("Function"));
if let Some(func_ty) = func_ty_opt {
let evaluator = BinaryOpEvaluator::new(self.ctx.types);
is_valid_rhs = evaluator.is_valid_instanceof_right_operand(
eval_right,
func_ty,
&mut |src, tgt| self.is_assignable_to(src, tgt),
);
} else if eval_right == TypeId::ANY
|| eval_right == TypeId::UNKNOWN
|| eval_right == TypeId::FUNCTION
{
is_valid_rhs = true;
}
if !is_valid_rhs && let Some(right_node) = self.ctx.arena.get(right_idx) {
let message = format_message(
diagnostic_messages::THE_RIGHT_HAND_SIDE_OF_AN_INSTANCEOF_EXPRESSION_MUST_BE_EITHER_OF_TYPE_ANY_A_CLA,
&[],
);
self.ctx.diagnostics.push(Diagnostic::error(
self.ctx.file_name.clone(),
right_node.pos,
right_node.end - right_node.pos,
message,
diagnostic_codes::THE_RIGHT_HAND_SIDE_OF_AN_INSTANCEOF_EXPRESSION_MUST_BE_EITHER_OF_TYPE_ANY_A_CLA,
));
}
}
type_stack.push(TypeId::BOOLEAN);
continue;
}
// Logical AND: `a && b`
if op_kind == SyntaxKind::AmpersandAmpersandToken as u16 {
self.check_truthy_or_falsy(left_idx);
if left_type == TypeId::ERROR || right_type == TypeId::ERROR {
type_stack.push(TypeId::ERROR);
continue;
}
let result = match evaluator.evaluate(left_type, right_type, "&&") {
BinaryOpResult::Success(ty) => ty,
BinaryOpResult::TypeError { .. } => TypeId::UNKNOWN,
};
type_stack.push(result);
continue;
}
// Logical OR: `a || b`
if op_kind == SyntaxKind::BarBarToken as u16 {
// TS2872/TS2873: left side of `||` can be syntactically always truthy/falsy.
self.check_truthy_or_falsy(left_idx);
if left_type == TypeId::ERROR || right_type == TypeId::ERROR {
type_stack.push(TypeId::ERROR);
continue;
}
let result = match evaluator.evaluate(left_type, right_type, "||") {
BinaryOpResult::Success(ty) => ty,
BinaryOpResult::TypeError { .. } => TypeId::UNKNOWN,
};
type_stack.push(result);
continue;
}
// Nullish coalescing: `a ?? b`
if op_kind == SyntaxKind::QuestionQuestionToken as u16 {
// TS2872: This kind of expression is always truthy.
self.check_always_truthy(left_idx, left_type);
// Propagate error types (don't collapse to unknown)
if left_type == TypeId::ERROR || right_type == TypeId::ERROR {
type_stack.push(TypeId::ERROR);
continue;
}
let (non_nullish, cause) = self.split_nullish_type(left_type);
if cause.is_none() {
type_stack.push(left_type);
} else {
let result = match non_nullish {
None => right_type,
Some(non_nullish) => factory.union(vec![non_nullish, right_type]),
};
type_stack.push(result);
}
continue;
}
// TS17006: Unary expression not allowed as left-hand side of `**`.
// `-x ** y` is ambiguous, so TSC forbids it. The parser produces
// Binary(PrefixUnary(-, x), **, y), so check if left_idx is a unary.
if op_kind == SyntaxKind::AsteriskAsteriskToken as u16 {
if let Some(left_node) = self.ctx.arena.get(left_idx)
&& left_node.kind == syntax_kind_ext::PREFIX_UNARY_EXPRESSION
&& let Some(left_unary) = self.ctx.arena.get_unary_expr(left_node)
{
let op_name = match left_unary.operator {
k if k == SyntaxKind::MinusToken as u16 => Some("-"),
k if k == SyntaxKind::PlusToken as u16 => Some("+"),
k if k == SyntaxKind::TildeToken as u16 => Some("~"),
k if k == SyntaxKind::ExclamationToken as u16 => Some("!"),
k if k == SyntaxKind::TypeOfKeyword as u16 => Some("typeof"),
k if k == SyntaxKind::VoidKeyword as u16 => Some("void"),
k if k == SyntaxKind::DeleteKeyword as u16 => Some("delete"),
_ => None,
};
if let Some(op_name) = op_name {
self.error_at_node_msg(
left_idx,
crate::diagnostics::diagnostic_codes::AN_UNARY_EXPRESSION_WITH_THE_OPERATOR_IS_NOT_ALLOWED_IN_THE_LEFT_HAND_SIDE_OF_AN,
&[op_name],
);
}
}
// TS2791: bigint exponentiation requires target >= ES2016.
// Only fire when both types are specifically bigint-like,
// not when either is `any`/`unknown` (TSC skips the bigint branch for those).
if (self.ctx.compiler_options.target as u32)
< (tsz_common::common::ScriptTarget::ES2016 as u32)
&& left_type != TypeId::ANY
&& right_type != TypeId::ANY
&& left_type != TypeId::UNKNOWN
&& right_type != TypeId::UNKNOWN
&& self.is_subtype_of(left_type, TypeId::BIGINT)
&& self.is_subtype_of(right_type, TypeId::BIGINT)
{
self.error_at_node_msg(
node_idx,
crate::diagnostics::diagnostic_codes::EXPONENTIATION_CANNOT_BE_PERFORMED_ON_BIGINT_VALUES_UNLESS_THE_TARGET_OPTION_IS,
&[],
);
}
}
// TS2367: Check for comparisons with no overlap
let is_equality_op = matches!(
op_kind,
k if k == SyntaxKind::EqualsEqualsToken as u16
|| k == SyntaxKind::ExclamationEqualsToken as u16
|| k == SyntaxKind::EqualsEqualsEqualsToken as u16
|| k == SyntaxKind::ExclamationEqualsEqualsToken as u16
);
// For TS2367, get the narrow types (literals) not the widened types
let left_narrow = self
.literal_type_from_initializer(left_idx)
.unwrap_or(left_type);
let right_narrow = self
.literal_type_from_initializer(right_idx)
.unwrap_or(right_type);
let is_left_nan = self.is_identifier_reference_to_global_nan(left_idx);
let is_right_nan = self.is_identifier_reference_to_global_nan(right_idx);
if is_equality_op && (is_left_nan || is_right_nan) {
let condition_result = match op_kind {
k if k == SyntaxKind::EqualsEqualsToken as u16
|| k == SyntaxKind::EqualsEqualsEqualsToken as u16 =>
{
"false"
}
_ => "true",
};
use crate::diagnostics::{diagnostic_codes, diagnostic_messages, format_message};
let message = format_message(
diagnostic_messages::THIS_CONDITION_WILL_ALWAYS_RETURN,
&[condition_result],
);
self.error_at_node(
node_idx,
&message,
diagnostic_codes::THIS_CONDITION_WILL_ALWAYS_RETURN,
);
} else if is_equality_op
&& left_narrow != TypeId::ERROR
&& right_narrow != TypeId::ERROR
&& left_narrow != TypeId::NEVER
&& right_narrow != TypeId::NEVER
&& self.types_have_no_overlap(left_narrow, right_narrow)
{
use crate::diagnostics::{diagnostic_codes, diagnostic_messages, format_message};
let left_str = self.format_type(left_narrow);
let right_str = self.format_type(right_narrow);
let message = format_message(
diagnostic_messages::THIS_COMPARISON_APPEARS_TO_BE_UNINTENTIONAL_BECAUSE_THE_TYPES_AND_HAVE_NO_OVERLA,
&[&left_str, &right_str],
);
self.error_at_node(
node_idx,
&message,
diagnostic_codes::THIS_COMPARISON_APPEARS_TO_BE_UNINTENTIONAL_BECAUSE_THE_TYPES_AND_HAVE_NO_OVERLA,
);
}
let op_str = match op_kind {
k if k == SyntaxKind::PlusToken as u16 => "+",
k if k == SyntaxKind::MinusToken as u16 => "-",
k if k == SyntaxKind::AsteriskToken as u16 => "*",
k if k == SyntaxKind::AsteriskAsteriskToken as u16 => "**",
k if k == SyntaxKind::SlashToken as u16 => "/",
k if k == SyntaxKind::PercentToken as u16 => "%",
k if k == SyntaxKind::LessThanToken as u16 => "<",
k if k == SyntaxKind::GreaterThanToken as u16 => ">",
k if k == SyntaxKind::LessThanEqualsToken as u16 => "<=",
k if k == SyntaxKind::GreaterThanEqualsToken as u16 => ">=",
k if k == SyntaxKind::EqualsEqualsToken as u16 => "==",
k if k == SyntaxKind::ExclamationEqualsToken as u16 => "!=",
k if k == SyntaxKind::EqualsEqualsEqualsToken as u16 => "===",
k if k == SyntaxKind::ExclamationEqualsEqualsToken as u16 => "!==",
// && and || are handled above
k if k == SyntaxKind::AmpersandToken as u16
|| k == SyntaxKind::BarToken as u16
|| k == SyntaxKind::CaretToken as u16
|| k == SyntaxKind::LessThanLessThanToken as u16
|| k == SyntaxKind::GreaterThanGreaterThanToken as u16
|| k == SyntaxKind::GreaterThanGreaterThanGreaterThanToken as u16 =>
{
// Bitwise operators require integer operands (number, bigint, any, or enum)
// Emit TS2362/TS2363 if operands are not valid
let op_str = match op_kind {
k if k == SyntaxKind::AmpersandToken as u16 => "&",
k if k == SyntaxKind::BarToken as u16 => "|",
k if k == SyntaxKind::CaretToken as u16 => "^",
k if k == SyntaxKind::LessThanLessThanToken as u16 => "<<",
k if k == SyntaxKind::GreaterThanGreaterThanToken as u16 => ">>",
k if k == SyntaxKind::GreaterThanGreaterThanGreaterThanToken as u16 => {
">>>"
}
_ => "?",
};
let emitted_nullish_error = self.check_and_emit_nullish_binary_operands(
left_idx, right_idx, left_type, right_type, op_str,
);
// Evaluate types to resolve unevaluated conditional/mapped types
let eval_left = self.evaluate_type_for_binary_ops(left_type);
let eval_right = self.evaluate_type_for_binary_ops(right_type);
let right_narrow = self
.literal_type_from_initializer(right_idx)
.unwrap_or(eval_right);
if matches!(op_str, "<<" | ">>" | ">>>")
&& let Some(n) = tsz_solver::type_queries_extended::get_number_literal_value(
self.ctx.types,
right_narrow,
)
&& n.abs() >= 32.0
{
let left_text = if let Some(left_node) = self.ctx.arena.get(left_idx) {
if let Some(src) = self.ctx.arena.source_files.first() {
src.text
.get(left_node.pos as usize..left_node.end as usize)
.unwrap_or("expr")
.to_string()
} else {
"expr".to_string()
}
} else {
"expr".to_string()
};
let shift_amount = ((n as i64) % 32).to_string();
self.error_at_node_msg(
node_idx,
crate::diagnostics::diagnostic_codes::THIS_OPERATION_CAN_BE_SIMPLIFIED_THIS_SHIFT_IS_IDENTICAL_TO,
&[&left_text, op_str, &shift_amount],
);
}
let result = evaluator.evaluate(eval_left, eval_right, op_str);
let result_type = match result {
BinaryOpResult::Success(result_type) => result_type,
BinaryOpResult::TypeError { .. } => {
// Don't emit errors if either operand is ERROR - prevents cascading errors
if left_type != TypeId::ERROR && right_type != TypeId::ERROR {
// Emit appropriate error for arithmetic type mismatch
self.emit_binary_operator_error(
node_idx,
left_idx,
right_idx,
left_type,
right_type,
op_str,
emitted_nullish_error,
);
}
TypeId::UNKNOWN
}
};
type_stack.push(result_type);
continue;
}
_ => {
type_stack.push(TypeId::UNKNOWN);
continue;
}
};
// Check for boxed primitive types in arithmetic operations BEFORE evaluating types.
// Boxed types (Number, String, Boolean) are interface types from lib.d.ts
// and are NOT valid for arithmetic operations. We must check BEFORE calling
// evaluate_type_for_binary_ops because that function converts boxed types
// to primitives (Number → number), which would make our check fail.
let is_arithmetic_op = matches!(op_str, "+" | "-" | "*" | "/" | "%" | "**");
// TS18050: Emit errors for null/undefined operands BEFORE returning results or evaluating further
let emitted_nullish_error = self.check_and_emit_nullish_binary_operands(
left_idx, right_idx, left_type, right_type, op_str,
);
if is_arithmetic_op {
let left_is_nullish = left_type == TypeId::NULL || left_type == TypeId::UNDEFINED;
let right_is_nullish =
right_type == TypeId::NULL || right_type == TypeId::UNDEFINED;
let left_is_boxed = self.is_boxed_primitive_type(left_type);
let right_is_boxed = self.is_boxed_primitive_type(right_type);
// If one operand is null/undefined and strict_null_checks is on, tsc prioritizes TS18050
// over the boxed primitive error (TS2362/TS2363/TS2365).
let skip_boxed_error = self.ctx.compiler_options.strict_null_checks
&& (left_is_nullish || right_is_nullish);
if (left_is_boxed || right_is_boxed) && !skip_boxed_error {
// Emit appropriate error based on operator
if op_str == "+" {
// TS2365: Operator '+' cannot be applied to types 'T' and 'U'
// Use the existing error reporter which handles + specially
let left_str = self.format_type(left_type);
let right_str = self.format_type(right_type);
if let Some(node) = self.ctx.arena.get(node_idx) {
let message = format!(
"Operator '{op_str}' cannot be applied to types '{left_str}' and '{right_str}'."
);
self.ctx.error(
node.pos,
node.end - node.pos,
message,
2365, // TS2365
);
}
} else {
// TS2362/TS2363: Left/right hand side must be number/bigint/enum
// Emit separate errors for left and right operands
if left_is_boxed && let Some(node) = self.ctx.arena.get(left_idx) {
let message = "The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.".to_string();
self.ctx.error(
node.pos,
node.end - node.pos,
message,
2362, // TS2362
);
}
if right_is_boxed && let Some(node) = self.ctx.arena.get(right_idx) {
let message = "The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.".to_string();
self.ctx.error(
node.pos,
node.end - node.pos,
message,
2363, // TS2363
);
}
}
type_stack.push(TypeId::UNKNOWN);
continue;
}
}
// Evaluate types to resolve unevaluated conditional/mapped types before
// passing to the solver. e.g., DeepPartial<number> | number → number
let eval_left = self.evaluate_type_for_binary_ops(left_type);
let eval_right = self.evaluate_type_for_binary_ops(right_type);
let result = evaluator.evaluate(eval_left, eval_right, op_str);
let result_type = match result {
BinaryOpResult::Success(result_type) => result_type,
BinaryOpResult::TypeError { left, right, op } => {
// Check if this is actually valid because we have enum types
// The evaluator doesn't have access to symbol information, so it can't
// detect enum types. We need to check here at the checker layer.
let left_is_enum = self.is_enum_type(left_type);
let right_is_enum = self.is_enum_type(right_type);
let is_arithmetic_op = matches!(op_str, "+" | "-" | "*" | "/" | "%" | "**");
// If both operands are enum types and this is an arithmetic operation,
// treat it as valid (enum members are numbers for numeric enums)
if is_arithmetic_op && left_is_enum && right_is_enum {
// For + operation, result is number; for other ops, also number
TypeId::NUMBER
} else if is_arithmetic_op
&& left_is_enum
&& evaluator.is_arithmetic_operand(right)
{
// Enum op number => number
TypeId::NUMBER
} else if is_arithmetic_op
&& right_is_enum
&& evaluator.is_arithmetic_operand(left)
{
// Number op enum => number
TypeId::NUMBER
} else {
// For equality operators (==, !=, ===, !==), tsc allows comparison
// when the types are comparable (assignable in either direction).
// For relational operators (<, >, <=, >=), tsc allows comparison
// if both are assignable to number/bigint, or if neither are, they must
// be comparable.
let is_comparable = if matches!(op_str, "==" | "!=" | "===" | "!==") {
self.is_type_comparable_to(eval_left, eval_right)
} else if matches!(op_str, "<" | ">" | "<=" | ">=") {
if eval_left == TypeId::ANY || eval_right == TypeId::ANY {
true
} else {
let number_or_bigint =
self.ctx.types.union(vec![TypeId::NUMBER, TypeId::BIGINT]);
let left_to_num =
self.is_assignable_to(eval_left, number_or_bigint);
let right_to_num =
self.is_assignable_to(eval_right, number_or_bigint);
if left_to_num && right_to_num {
true
} else if !left_to_num && !right_to_num {
self.is_type_comparable_to(eval_left, eval_right)
} else {
false
}
}
} else {
false
};
if is_comparable {
TypeId::BOOLEAN
} else {
// Don't emit errors if either operand is ERROR - prevents cascading errors
if left != TypeId::ERROR && right != TypeId::ERROR {
// Use original types for error messages (more informative)
self.emit_binary_operator_error(
node_idx,
left_idx,
right_idx,
left_type,
right_type,
op,
emitted_nullish_error,
);
}
TypeId::UNKNOWN
}
}
}
};
// Check for type overlap for equality/inequality operators (TS2367)
let is_equality_op = matches!(
op_kind,
k if k == SyntaxKind::EqualsEqualsToken as u16
|| k == SyntaxKind::EqualsEqualsEqualsToken as u16
);
let is_inequality_op = matches!(
op_kind,
k if k == SyntaxKind::ExclamationEqualsToken as u16
|| k == SyntaxKind::ExclamationEqualsEqualsToken as u16
);
if is_equality_op || is_inequality_op {
let is_left_nan = self.is_identifier_reference_to_global_nan(left_idx);
let is_right_nan = self.is_identifier_reference_to_global_nan(right_idx);
// Check if the types have any overlap (skip if NaN, handled above)
// Also skip if either type is `never` — tsc doesn't emit TS2367 for never.
if !is_left_nan
&& !is_right_nan
&& left_type != TypeId::NEVER
&& right_type != TypeId::NEVER
&& !self.are_types_overlapping(left_type, right_type)
{
// TS2367: This condition will always return 'false'/'true'
self.error_comparison_no_overlap(
left_type,
right_type,
is_equality_op,
node_idx,
);
}
}
type_stack.push(result_type);
}
type_stack.pop().unwrap_or(TypeId::UNKNOWN)
}
}