tsz-checker 0.1.9

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

use crate::query_boundaries::dispatch as query;
use crate::query_boundaries::generic_checker as generic_query;
use crate::state::CheckerState;
use tsz_parser::parser::NodeIndex;
use tsz_parser::parser::node::NodeAccess;
use tsz_parser::parser::syntax_kind_ext;
use tsz_scanner::SyntaxKind;
use tsz_solver::TypeId;

/// Dispatcher for expression type computation.
///
/// `ExpressionDispatcher` handles the dispatch of type computation for different
/// node kinds, delegating to specialized methods in `CheckerState`.
pub struct ExpressionDispatcher<'a, 'b> {
    /// Reference to the checker state.
    pub checker: &'a mut CheckerState<'b>,
}

impl<'a, 'b> ExpressionDispatcher<'a, 'b> {
    /// Create a new expression dispatcher.
    pub const fn new(checker: &'a mut CheckerState<'b>) -> Self {
        Self { checker }
    }

    /// Resolve a literal type: preserve the narrow literal if we're in a const
    /// assertion, contextual typing expects it, or we're computing a type for
    /// a compound expression (conditional/logical) that should preserve literals.
    fn resolve_literal(&mut self, literal_type: Option<TypeId>, widened: TypeId) -> TypeId {
        match literal_type {
            Some(lit)
                if self.checker.ctx.in_const_assertion
                    || self.checker.ctx.preserve_literal_types
                    || self.checker.contextual_literal_type(lit).is_some() =>
            {
                lit
            }
            _ => widened,
        }
    }

    fn get_expected_yield_type(&mut self, idx: NodeIndex) -> Option<TypeId> {
        let enclosing_fn_idx = self.checker.find_enclosing_function(idx)?;
        let fn_node = self.checker.ctx.arena.get(enclosing_fn_idx)?;

        let declared_return_type_node =
            if let Some(func) = self.checker.ctx.arena.get_function(fn_node) {
                if !func.asterisk_token || func.type_annotation.is_none() {
                    return None;
                }
                func.type_annotation
            } else if let Some(method) = self.checker.ctx.arena.get_method_decl(fn_node) {
                if !method.asterisk_token || method.type_annotation.is_none() {
                    return None;
                }
                method.type_annotation
            } else {
                return None;
            };

        // Prefer syntactic extraction from the explicit annotation first.
        // This preserves `TYield` exactly as written (e.g. `IterableIterator<number>`
        // => `number`) even if semantic base resolution currently widens it.
        let declared_return_node = self.checker.ctx.arena.get(declared_return_type_node)?;
        if declared_return_node.kind != syntax_kind_ext::TYPE_REFERENCE {
            let declared_return_type = self
                .checker
                .get_type_from_type_node(declared_return_type_node);
            return self
                .checker
                .get_generator_yield_type_argument(declared_return_type);
        }
        let type_ref = self.checker.ctx.arena.get_type_ref(declared_return_node)?;
        let type_name_node = self.checker.ctx.arena.get(type_ref.type_name)?;
        let type_name = self
            .checker
            .ctx
            .arena
            .get_identifier(type_name_node)
            .map(|ident| ident.escaped_text.as_str())?;

        if !matches!(
            type_name,
            "Generator"
                | "AsyncGenerator"
                | "Iterator"
                | "AsyncIterator"
                | "IterableIterator"
                | "AsyncIterableIterator"
        ) {
            let declared_return_type = self
                .checker
                .get_type_from_type_node(declared_return_type_node);
            return self
                .checker
                .get_generator_yield_type_argument(declared_return_type);
        }

        if let Some(first_arg) = type_ref
            .type_arguments
            .as_ref()
            .and_then(|args| args.nodes.first().copied())
        {
            return Some(self.checker.get_type_from_type_node(first_arg));
        }

        let declared_return_type = self
            .checker
            .get_type_from_type_node(declared_return_type_node);
        self.checker
            .get_generator_yield_type_argument(declared_return_type)
    }

    fn type_node_includes_undefined(&self, idx: NodeIndex) -> bool {
        let Some(node) = self.checker.ctx.arena.get(idx) else {
            return false;
        };

        if node.kind == SyntaxKind::UndefinedKeyword as u16 {
            return true;
        }

        if node.kind == syntax_kind_ext::UNION_TYPE
            && let Some(composite) = self.checker.ctx.arena.get_composite_type(node)
        {
            return composite
                .types
                .nodes
                .iter()
                .copied()
                .any(|member| self.type_node_includes_undefined(member));
        }

        false
    }

    fn explicit_generator_yield_allows_undefined(&mut self, idx: NodeIndex) -> Option<bool> {
        let enclosing_fn_idx = self.checker.find_enclosing_function(idx)?;
        let fn_node = self.checker.ctx.arena.get(enclosing_fn_idx)?;

        let declared_return_type_node =
            if let Some(func) = self.checker.ctx.arena.get_function(fn_node) {
                if !func.asterisk_token || func.type_annotation.is_none() {
                    return None;
                }
                func.type_annotation
            } else if let Some(method) = self.checker.ctx.arena.get_method_decl(fn_node) {
                if !method.asterisk_token || method.type_annotation.is_none() {
                    return None;
                }
                method.type_annotation
            } else {
                return None;
            };

        let declared_return_node = self.checker.ctx.arena.get(declared_return_type_node)?;
        if declared_return_node.kind != syntax_kind_ext::TYPE_REFERENCE {
            return None;
        }
        let type_ref = self.checker.ctx.arena.get_type_ref(declared_return_node)?;
        let type_name_node = self.checker.ctx.arena.get(type_ref.type_name)?;
        let type_name = self
            .checker
            .ctx
            .arena
            .get_identifier(type_name_node)
            .map(|ident| ident.escaped_text.as_str())?;
        if !matches!(
            type_name,
            "Generator"
                | "AsyncGenerator"
                | "Iterator"
                | "AsyncIterator"
                | "IterableIterator"
                | "AsyncIterableIterator"
        ) {
            return None;
        }

        let first_arg = type_ref.type_arguments.as_ref()?.nodes.first().copied()?;
        Some(self.type_node_includes_undefined(first_arg))
    }

    /// Get the declared generator type (`Generator<TYield, TReturn, TNext>`) for the enclosing generator function.
    /// Returns `None` if not in a generator or if the generator has no explicit type annotation.
    fn get_expected_generator_type(&mut self, idx: NodeIndex) -> Option<TypeId> {
        let enclosing_fn_idx = self.checker.find_enclosing_function(idx)?;
        let fn_node = self.checker.ctx.arena.get(enclosing_fn_idx)?;

        let declared_return_type_node =
            if let Some(func) = self.checker.ctx.arena.get_function(fn_node) {
                if !func.asterisk_token || func.type_annotation.is_none() {
                    return None;
                }
                func.type_annotation
            } else if let Some(method) = self.checker.ctx.arena.get_method_decl(fn_node) {
                if !method.asterisk_token || method.type_annotation.is_none() {
                    return None;
                }
                method.type_annotation
            } else {
                return None;
            };

        // Get the declared generator type
        let declared_return_type = self
            .checker
            .get_type_from_type_node(declared_return_type_node);
        Some(declared_return_type)
    }

    fn get_type_of_yield_expression(&mut self, idx: NodeIndex) -> TypeId {
        let Some(node) = self.checker.ctx.arena.get(idx) else {
            return TypeId::ERROR;
        };
        let Some(yield_expr) = self.checker.ctx.arena.get_unary_expr_ex(node) else {
            return TypeId::ERROR;
        };

        // If yield is outside a generator function, the parser already emitted TS1163.
        // Return ANY without evaluating the operand to avoid cascading TS2304 errors.
        let is_in_generator = self
            .checker
            .find_enclosing_function(idx)
            .and_then(|fn_idx| self.checker.ctx.arena.get(fn_idx))
            .is_some_and(|fn_node| {
                if let Some(func) = self.checker.ctx.arena.get_function(fn_node) {
                    func.asterisk_token
                } else if let Some(method) = self.checker.ctx.arena.get_method_decl(fn_node) {
                    method.asterisk_token
                } else {
                    false
                }
            });
        if !is_in_generator {
            return TypeId::ANY;
        }

        let yielded_type = if yield_expr.expression.is_none() {
            TypeId::UNDEFINED
        } else {
            // Set contextual type for yield expression from the generator's yield type.
            // This allows `yield (num) => ...` to contextually type arrow params.
            // For `yield *expr`, the expression is an iterable of the yield type,
            // so wrap the contextual type in Array<T> to contextually type array elements.
            let prev_contextual = self.checker.ctx.contextual_type;
            if let Some(yield_ctx) = self.checker.ctx.current_yield_type() {
                if yield_expr.asterisk_token {
                    // yield *[x => ...] needs Array<TYield> as contextual type
                    // so each array element gets TYield as its contextual type
                    let array_of_yield = self.checker.ctx.types.factory().array(yield_ctx);
                    self.checker.ctx.contextual_type = Some(array_of_yield);
                } else {
                    self.checker.ctx.contextual_type = Some(yield_ctx);
                }
            }
            let expression_type = self.checker.get_type_of_node(yield_expr.expression);
            self.checker.ctx.contextual_type = prev_contextual;
            if yield_expr.asterisk_token {
                let is_async_generator = self
                    .checker
                    .find_enclosing_function(idx)
                    .and_then(|fn_idx| self.checker.ctx.arena.get(fn_idx))
                    .is_some_and(|fn_node| {
                        if let Some(func) = self.checker.ctx.arena.get_function(fn_node) {
                            func.is_async && func.asterisk_token
                        } else if let Some(method) = self.checker.ctx.arena.get_method_decl(fn_node)
                        {
                            self.checker.has_async_modifier(&method.modifiers)
                                && method.asterisk_token
                        } else {
                            false
                        }
                    });

                use crate::diagnostics::{diagnostic_codes, diagnostic_messages, format_message};
                if is_async_generator {
                    let is_iterable = self.checker.is_async_iterable_type(expression_type)
                        || self.checker.is_iterable_type(expression_type);
                    if !is_iterable {
                        let type_str = self.checker.format_type(expression_type);
                        let message = format_message(
                            diagnostic_messages::TYPE_MUST_HAVE_A_SYMBOL_ASYNCITERATOR_METHOD_THAT_RETURNS_AN_ASYNC_ITERATOR,
                            &[&type_str],
                        );
                        self.checker.error_at_node(
                            yield_expr.expression,
                            &message,
                            diagnostic_codes::TYPE_MUST_HAVE_A_SYMBOL_ASYNCITERATOR_METHOD_THAT_RETURNS_AN_ASYNC_ITERATOR,
                        );
                    }
                } else {
                    let is_iterable = self.checker.is_iterable_type(expression_type);
                    if !is_iterable {
                        let type_str = self.checker.format_type(expression_type);
                        let message = format_message(
                            diagnostic_messages::TYPE_MUST_HAVE_A_SYMBOL_ITERATOR_METHOD_THAT_RETURNS_AN_ITERATOR,
                            &[&type_str],
                        );
                        self.checker.error_at_node(
                            yield_expr.expression,
                            &message,
                            diagnostic_codes::TYPE_MUST_HAVE_A_SYMBOL_ITERATOR_METHOD_THAT_RETURNS_AN_ITERATOR,
                        );
                    }
                }

                if is_async_generator {
                    tsz_solver::operations::get_async_iterable_element_type(
                        self.checker.ctx.types,
                        expression_type,
                    )
                } else {
                    tsz_solver::operations::get_iterator_info(
                        self.checker.ctx.types,
                        expression_type,
                        false,
                    )
                    .map_or(TypeId::ANY, |info| info.yield_type)
                }
            } else {
                expression_type
            }
        };

        if let Some(expected_yield_type) = self.get_expected_yield_type(idx) {
            let error_node = if yield_expr.expression.is_none() {
                idx
            } else {
                yield_expr.expression
            };

            self.checker.ensure_relation_input_ready(yielded_type);
            self.checker
                .ensure_relation_input_ready(expected_yield_type);

            let resolved_expected_yield_type = self.checker.resolve_lazy_type(expected_yield_type);
            let syntactic_yield_allows_undefined = self
                .explicit_generator_yield_allows_undefined(idx)
                .unwrap_or(false);

            let bare_yield_requires_error = yield_expr.expression.is_none()
                && expected_yield_type != TypeId::ANY
                && expected_yield_type != TypeId::UNKNOWN
                && expected_yield_type != TypeId::ERROR
                && expected_yield_type != TypeId::VOID  // Allow bare yield for void
                && !syntactic_yield_allows_undefined
                && !tsz_solver::type_queries::type_includes_undefined(self.checker.ctx.types, expected_yield_type)
                && !tsz_solver::type_queries::type_includes_undefined(self.checker.ctx.types, resolved_expected_yield_type);

            // TS delegates nuanced `yield*` compatibility through iterator protocols.
            // Avoid direct TS2322 checks here to prevent false positives.
            if yield_expr.asterisk_token {
                return TypeId::ANY;
            }

            if bare_yield_requires_error {
                self.checker.check_assignable_or_report(
                    yielded_type,
                    expected_yield_type,
                    error_node,
                );
            } else if !self.checker.type_contains_error(expected_yield_type)
                && !self.checker.check_assignable_or_report(
                    yielded_type,
                    expected_yield_type,
                    error_node,
                )
            {
                // Diagnostic emitted by check_assignable_or_report.
            }
        }

        // TypeScript models `yield` result type as the value received by `.next(...)` (TNext).
        // Extract TNext from Generator<TYield, TReturn, TNext> or AsyncGenerator<TYield, TReturn, TNext>.
        if let Some(generator_type) = self.get_expected_generator_type(idx) {
            // Use contextual helper to extract TNext (argument index 2)
            let ctx = tsz_solver::ContextualTypeContext::with_expected(
                self.checker.ctx.types,
                generator_type,
            );
            if let Some(next_type) = ctx.get_generator_next_type() {
                return next_type;
            }
        }

        // Fallback to `any` if no generator context is available.
        // Emit TS7057 when noImplicitAny is enabled, the generator lacks a return type,
        // and the yield result is consumed (not discarded).
        if self.checker.ctx.no_implicit_any() && !self.expression_result_is_unused(idx) {
            // Check if there is a contextual type that would prevent the implicit any
            let contextual = self.checker.ctx.contextual_type;
            if contextual.is_none() || contextual == Some(TypeId::ANY) {
                use crate::diagnostics::{diagnostic_codes, diagnostic_messages};
                self.checker.error_at_node(
                    idx,
                    diagnostic_messages::YIELD_EXPRESSION_IMPLICITLY_RESULTS_IN_AN_ANY_TYPE_BECAUSE_ITS_CONTAINING_GENERA,
                    diagnostic_codes::YIELD_EXPRESSION_IMPLICITLY_RESULTS_IN_AN_ANY_TYPE_BECAUSE_ITS_CONTAINING_GENERA,
                );
            }
        }
        TypeId::ANY
    }

    /// Check if an expression's result value is unused (discarded).
    /// Mirrors TypeScript's `expressionResultIsUnused` from utilities.ts.
    fn expression_result_is_unused(&self, idx: NodeIndex) -> bool {
        let mut current = idx;
        loop {
            let Some(ext) = self.checker.ctx.arena.get_extended(current) else {
                return false;
            };
            let parent_idx = ext.parent;
            let Some(parent) = self.checker.ctx.arena.get(parent_idx) else {
                return false;
            };

            // Walk up through parenthesized expressions
            if parent.kind == syntax_kind_ext::PARENTHESIZED_EXPRESSION {
                current = parent_idx;
                continue;
            }

            // Expression statement: result is unused
            if parent.kind == syntax_kind_ext::EXPRESSION_STATEMENT {
                return true;
            }

            // Void expression: result is unused.
            // Our parser models `void expr` as PREFIX_UNARY_EXPRESSION with VoidKeyword operator.
            if parent.kind == syntax_kind_ext::VOID_EXPRESSION {
                return true;
            }
            if parent.kind == syntax_kind_ext::PREFIX_UNARY_EXPRESSION
                && let Some(unary) = self.checker.ctx.arena.get_unary_expr(parent)
                && unary.operator == SyntaxKind::VoidKeyword as u16
            {
                return true;
            }

            // For statement: initializer and incrementor results are unused
            if parent.kind == syntax_kind_ext::FOR_STATEMENT {
                if let Some(loop_data) = self.checker.ctx.arena.get_loop(parent)
                    && (loop_data.initializer == current || loop_data.incrementor == current)
                {
                    return true;
                }
                return false;
            }

            // Binary comma expression: left side is always unused;
            // right side is unused if the parent comma expression is unused
            if parent.kind == syntax_kind_ext::BINARY_EXPRESSION
                && let Some(bin) = self.checker.ctx.arena.get_binary_expr(parent)
                && bin.operator_token == SyntaxKind::CommaToken as u16
            {
                if current == bin.left {
                    return true;
                }
                // Right side: walk up to check if parent is unused
                current = parent_idx;
                continue;
            }

            return false;
        }
    }

    /// Dispatch type computation based on node kind.
    ///
    /// This method examines the syntax node kind and dispatches to the
    /// appropriate specialized type computation method.
    pub fn dispatch_type_computation(&mut self, idx: NodeIndex) -> TypeId {
        let Some(node) = self.checker.ctx.arena.get(idx) else {
            return TypeId::ERROR; // Missing node - propagate error
        };
        match node.kind {
            // Identifiers
            k if k == SyntaxKind::Identifier as u16 => self.checker.get_type_of_identifier(idx),
            k if k == SyntaxKind::ThisKeyword as u16 => {
                {
                    use crate::diagnostics::{diagnostic_codes, diagnostic_messages};
                    // TS2465: 'this' cannot be referenced in a computed property name.
                    // Check this first — it takes priority over other `this` errors.
                    if self
                        .checker
                        .is_this_in_class_member_computed_property_name(idx)
                    {
                        self.checker.error_at_node(
                            idx,
                            diagnostic_messages::THIS_CANNOT_BE_REFERENCED_IN_A_COMPUTED_PROPERTY_NAME,
                            diagnostic_codes::THIS_CANNOT_BE_REFERENCED_IN_A_COMPUTED_PROPERTY_NAME,
                        );
                        return TypeId::ANY;
                    }
                    // TS2331: 'this' cannot be referenced in a module or namespace body
                    if self.checker.is_this_in_namespace_body(idx) {
                        self.checker.error_at_node(
                            idx,
                            diagnostic_messages::THIS_CANNOT_BE_REFERENCED_IN_A_MODULE_OR_NAMESPACE_BODY,
                            diagnostic_codes::THIS_CANNOT_BE_REFERENCED_IN_A_MODULE_OR_NAMESPACE_BODY,
                        );
                        return TypeId::ANY;
                    }
                    // TS17009: 'super' must be called before accessing 'this'
                    if self
                        .checker
                        .is_this_before_super_in_derived_constructor(idx)
                    {
                        self.checker.error_at_node(
                            idx,
                            diagnostic_messages::SUPER_MUST_BE_CALLED_BEFORE_ACCESSING_THIS_IN_THE_CONSTRUCTOR_OF_A_DERIVED_CLASS,
                            diagnostic_codes::SUPER_MUST_BE_CALLED_BEFORE_ACCESSING_THIS_IN_THE_CONSTRUCTOR_OF_A_DERIVED_CLASS,
                        );
                    }
                }
                if let Some(this_type) = self.checker.current_this_type() {
                    self.checker.apply_flow_narrowing(idx, this_type)
                } else if let Some(ref class_info) = self.checker.ctx.enclosing_class {
                    // Inside a class but no explicit this type on stack -
                    // return the class instance type (e.g., for constructor default params)
                    if let Some(class_node) = self.checker.ctx.arena.get(class_info.class_idx)
                        && let Some(class_data) = self.checker.ctx.arena.get_class(class_node)
                    {
                        let class_instance = self
                            .checker
                            .get_class_instance_type(class_info.class_idx, class_data);
                        return self.checker.apply_flow_narrowing(idx, class_instance);
                    }
                    TypeId::ANY
                } else if self.checker.this_has_contextual_owner(idx).is_some() {
                    // `this` in a class or object literal member but enclosing_class
                    // not yet set. Suppress TS2683 - `this` is contextually typed.
                    TypeId::ANY
                } else if self.checker.ctx.no_implicit_this()
                    && !self.checker.is_js_file()
                    && self
                        .checker
                        .find_enclosing_non_arrow_function(idx)
                        .is_some()
                {
                    // TS2683: 'this' implicitly has type 'any'
                    // Suppressed in JS files: tsc infers `this` for constructor/prototype
                    // patterns and JSDoc-typed functions.
                    use crate::diagnostics::{diagnostic_codes, diagnostic_messages};
                    self.checker.error_at_node(
                        idx,
                        diagnostic_messages::THIS_IMPLICITLY_HAS_TYPE_ANY_BECAUSE_IT_DOES_NOT_HAVE_A_TYPE_ANNOTATION,
                        diagnostic_codes::THIS_IMPLICITLY_HAS_TYPE_ANY_BECAUSE_IT_DOES_NOT_HAVE_A_TYPE_ANNOTATION,
                    );
                    TypeId::ANY
                } else {
                    TypeId::ANY
                }
            }
            k if k == SyntaxKind::SuperKeyword as u16 => {
                self.checker.get_type_of_super_keyword(idx)
            }

            // Literals — preserve literal types when contextual typing expects them.
            k if k == SyntaxKind::NumericLiteral as u16 => self.resolve_literal(
                self.checker.literal_type_from_initializer(idx),
                TypeId::NUMBER,
            ),
            k if k == SyntaxKind::BigIntLiteral as u16 => {
                // TS2737: bigint literals require target >= ES2020 in non-ambient contexts.
                if (self.checker.ctx.compiler_options.target as u32)
                    < (tsz_common::common::ScriptTarget::ES2020 as u32)
                    && !self.checker.is_ambient_declaration(idx)
                {
                    use crate::diagnostics::{diagnostic_codes, diagnostic_messages};
                    self.checker.error_at_node(
                        idx,
                        diagnostic_messages::BIGINT_LITERALS_ARE_NOT_AVAILABLE_WHEN_TARGETING_LOWER_THAN_ES2020,
                        diagnostic_codes::BIGINT_LITERALS_ARE_NOT_AVAILABLE_WHEN_TARGETING_LOWER_THAN_ES2020,
                    );
                }
                self.resolve_literal(
                    self.checker.literal_type_from_initializer(idx),
                    TypeId::BIGINT,
                )
            }
            k if k == SyntaxKind::StringLiteral as u16 => self.resolve_literal(
                self.checker.literal_type_from_initializer(idx),
                TypeId::STRING,
            ),
            k if k == SyntaxKind::TrueKeyword as u16 => {
                let literal_type = self.checker.ctx.types.literal_boolean(true);
                self.resolve_literal(Some(literal_type), TypeId::BOOLEAN)
            }
            k if k == SyntaxKind::FalseKeyword as u16 => {
                let literal_type = self.checker.ctx.types.literal_boolean(false);
                self.resolve_literal(Some(literal_type), TypeId::BOOLEAN)
            }
            k if k == SyntaxKind::NullKeyword as u16 => TypeId::NULL,

            // Binary expressions
            k if k == syntax_kind_ext::BINARY_EXPRESSION => {
                self.checker.get_type_of_binary_expression(idx)
            }

            // Call expressions
            k if k == syntax_kind_ext::CALL_EXPRESSION => {
                self.checker.get_type_of_call_expression(idx)
            }

            // Tagged template expressions (e.g., `tag\`hello ${x}\``)
            k if k == syntax_kind_ext::TAGGED_TEMPLATE_EXPRESSION => {
                self.checker.get_type_of_tagged_template_expression(idx)
            }

            // New expressions
            k if k == syntax_kind_ext::NEW_EXPRESSION => {
                self.checker.get_type_of_new_expression(idx)
            }

            // Class expressions
            k if k == syntax_kind_ext::CLASS_EXPRESSION => {
                if let Some(class) = self.checker.ctx.arena.get_class(node).cloned() {
                    self.checker.check_class_expression(idx, &class);

                    // When a class extends a type parameter and adds no new instance members,
                    // type it as the type parameter to maintain generic compatibility
                    if let Some(base_type_param) = self
                        .checker
                        .get_extends_type_parameter_if_transparent(&class)
                    {
                        base_type_param
                    } else {
                        self.checker.get_class_constructor_type(idx, &class)
                    }
                } else {
                    // Return ANY to prevent cascading TS2571 errors
                    TypeId::ANY
                }
            }

            // Property access
            k if k == syntax_kind_ext::PROPERTY_ACCESS_EXPRESSION => {
                self.checker.get_type_of_property_access(idx)
            }

            // Element access
            k if k == syntax_kind_ext::ELEMENT_ACCESS_EXPRESSION => {
                self.checker.get_type_of_element_access(idx)
            }

            // Conditional expression (ternary)
            k if k == syntax_kind_ext::CONDITIONAL_EXPRESSION => {
                self.checker.get_type_of_conditional_expression(idx)
            }

            // Variable declaration
            k if k == syntax_kind_ext::VARIABLE_DECLARATION => {
                self.checker.get_type_of_variable_declaration(idx)
            }

            // Function declaration
            k if k == syntax_kind_ext::FUNCTION_DECLARATION => {
                self.checker.get_type_of_function(idx)
            }

            // Function expression
            k if k == syntax_kind_ext::FUNCTION_EXPRESSION => {
                if self.checker.is_js_file() {
                    self.checker.check_js_grammar_function(idx, node);
                }
                // TS1100: Invalid use of 'eval'/'arguments' as function expression name
                if let Some(func) = self.checker.ctx.arena.get_function(node)
                    && func.name.is_some()
                    && let Some(name_node) = self.checker.ctx.arena.get(func.name)
                    && let Some(ident) = self.checker.ctx.arena.get_identifier(name_node)
                    && (ident.escaped_text == "eval" || ident.escaped_text == "arguments")
                    && self.checker.is_strict_mode_for_node(idx)
                {
                    let code = if self.checker.ctx.enclosing_class.is_some() {
                        crate::diagnostics::diagnostic_codes::CODE_CONTAINED_IN_A_CLASS_IS_EVALUATED_IN_JAVASCRIPTS_STRICT_MODE_WHICH_DOES_NOT
                    } else {
                        crate::diagnostics::diagnostic_codes::INVALID_USE_OF_IN_STRICT_MODE
                    };
                    self.checker
                        .error_at_node_msg(func.name, code, &[&ident.escaped_text]);
                }
                self.checker.get_type_of_function(idx)
            }

            // Arrow function
            k if k == syntax_kind_ext::ARROW_FUNCTION => {
                if self.checker.is_js_file() {
                    self.checker.check_js_grammar_function(idx, node);
                }
                self.checker.get_type_of_function(idx)
            }

            // Array literal
            k if k == syntax_kind_ext::ARRAY_LITERAL_EXPRESSION => {
                self.checker.get_type_of_array_literal(idx)
            }

            // Object literal
            k if k == syntax_kind_ext::OBJECT_LITERAL_EXPRESSION => {
                self.checker.get_type_of_object_literal(idx)
            }

            // Prefix unary expression
            k if k == syntax_kind_ext::PREFIX_UNARY_EXPRESSION => {
                self.checker.get_type_of_prefix_unary(idx)
            }

            // Postfix unary expression - ++ and -- require numeric operand and valid l-value
            k if k == syntax_kind_ext::POSTFIX_UNARY_EXPRESSION => {
                if let Some(unary) = self.checker.ctx.arena.get_unary_expr(node) {
                    // 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.checker.ctx.arena.get(unary.operand)
                        && operand_node.kind == SyntaxKind::Identifier as u16
                        && let Some(id_data) = self.checker.ctx.arena.get_identifier(operand_node)
                        && (id_data.escaped_text == "eval" || id_data.escaped_text == "arguments")
                        && self.checker.is_strict_mode_for_node(unary.operand)
                    {
                        use crate::diagnostics::diagnostic_codes;
                        let code = if self.checker.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.checker.error_at_node_msg(
                            unary.operand,
                            code,
                            &[&id_data.escaped_text],
                        );
                        emitted_strict = true;
                    }

                    // TSC checks arithmetic type BEFORE lvalue — if the type check
                    // fails (TS2356), the lvalue check (TS2357) is skipped.
                    let operand_type = self.checker.get_type_of_node(unary.operand);
                    let mut arithmetic_ok = true;

                    if !emitted_strict {
                        use tsz_solver::BinaryOpEvaluator;
                        let evaluator = BinaryOpEvaluator::new(self.checker.ctx.types);
                        let is_valid = evaluator.is_arithmetic_operand(operand_type);

                        if !is_valid {
                            arithmetic_ok = false;
                            use crate::diagnostics::{diagnostic_codes, diagnostic_messages};
                            self.checker.error_at_node(
                                unary.operand,
                                diagnostic_messages::AN_ARITHMETIC_OPERAND_MUST_BE_OF_TYPE_ANY_NUMBER_BIGINT_OR_AN_ENUM_TYPE,
                                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
                            .checker
                            .check_increment_decrement_operand(unary.operand);

                        if !emitted_lvalue {
                            // TS2588: Cannot assign to 'x' because it is a constant.
                            let is_const = self.checker.check_const_assignment(unary.operand);

                            // TS2630: Cannot assign to 'x' because it is a function.
                            self.checker.check_function_assignment(unary.operand);

                            // TS2540: Cannot assign to readonly property
                            if !is_const {
                                self.checker.check_readonly_assignment(unary.operand, idx);
                            }
                        }
                    }
                }

                TypeId::NUMBER
            }

            // typeof expression
            k if k == syntax_kind_ext::TYPE_OF_EXPRESSION => TypeId::STRING,

            // void expression
            k if k == syntax_kind_ext::VOID_EXPRESSION => TypeId::UNDEFINED,

            // await expression - unwrap Promise<T> to get T, with contextual typing (Phase 6 - tsz-3)
            k if k == syntax_kind_ext::AWAIT_EXPRESSION => {
                self.checker.get_type_of_await_expression(idx)
            }

            // yield expression
            k if k == syntax_kind_ext::YIELD_EXPRESSION => self.get_type_of_yield_expression(idx),

            // Parenthesized expression - just pass through to inner expression
            k if k == syntax_kind_ext::PARENTHESIZED_EXPRESSION => {
                if let Some(paren) = self.checker.ctx.arena.get_parenthesized(node) {
                    // Check if expression is missing (parse error: empty parentheses)
                    if paren.expression.is_none() {
                        // Parse error - return ERROR to suppress cascading errors
                        return TypeId::ERROR;
                    }
                    // In JS/checkJs, inline JSDoc casts like `/** @type {T} */(expr)`
                    // should behave as type assertions and produce the annotated type.
                    if let Some(jsdoc_type) = self.checker.jsdoc_type_annotation_for_node(idx) {
                        let _ = self.checker.get_type_of_node(paren.expression);
                        jsdoc_type
                    } else if let Some(satisfies_type) =
                        self.checker.jsdoc_satisfies_annotation_for_node(idx)
                    {
                        let expr_type = self.checker.get_type_of_node(paren.expression);
                        let _ = self.checker.check_satisfies_assignable_or_report(
                            expr_type,
                            satisfies_type,
                            paren.expression,
                        );
                        expr_type
                    } else {
                        self.checker.get_type_of_node(paren.expression)
                    }
                } else {
                    // Missing parenthesized data - propagate error
                    TypeId::ERROR
                }
            }

            // Type assertions / `as` / `satisfies`
            k if k == syntax_kind_ext::AS_EXPRESSION
                || k == syntax_kind_ext::SATISFIES_EXPRESSION
                || k == syntax_kind_ext::TYPE_ASSERTION =>
            {
                if let Some(assertion) = self.checker.ctx.arena.get_type_assertion(node) {
                    // Check for const assertion BEFORE type-checking the expression
                    // so we can set the context flag to preserve literal types
                    let is_const_assertion =
                        if let Some(type_node) = self.checker.ctx.arena.get(assertion.type_node) {
                            type_node.kind == tsz_scanner::SyntaxKind::ConstKeyword as u16
                                || (type_node.kind == syntax_kind_ext::TYPE_REFERENCE
                                    && self.checker.ctx.arena.get_type_ref(type_node).is_some_and(
                                        |type_ref| {
                                            type_ref.type_arguments.is_none()
                                                && self
                                                    .checker
                                                    .ctx
                                                    .arena
                                                    .get_identifier_text(type_ref.type_name)
                                                    .is_some_and(|name| name == "const")
                                        },
                                    ))
                        } else {
                            false
                        };

                    // Set the in_const_assertion flag to preserve literal types in nested expressions
                    let prev_in_const_assertion = self.checker.ctx.in_const_assertion;
                    if is_const_assertion {
                        self.checker.ctx.in_const_assertion = true;
                    }

                    // In recovery scenarios we may not have a type node; fall back to the expression type.
                    if assertion.type_node.is_none() {
                        let expr_type = self.checker.get_type_of_node(assertion.expression);
                        self.checker.ctx.in_const_assertion = prev_in_const_assertion;
                        expr_type
                    } else if is_const_assertion {
                        let expr_type = self.checker.get_type_of_node(assertion.expression);
                        self.checker.ctx.in_const_assertion = prev_in_const_assertion;
                        use tsz_solver::widening::apply_const_assertion;
                        apply_const_assertion(self.checker.ctx.types, expr_type)
                    } else {
                        // Check for duplicate properties in type literal nodes (TS2300)
                        self.checker
                            .check_type_for_parameter_properties(assertion.type_node);

                        let asserted_type =
                            self.checker.get_type_from_type_node(assertion.type_node);

                        // Set contextual type before checking expression for both
                        // type assertions and `satisfies`. This enables contextual typing
                        // for lambdas, object literals, etc. inside `<T>(expr)` / `expr as T` / `expr satisfies T`.
                        let prev_contextual_type = self.checker.ctx.contextual_type;
                        if !is_const_assertion {
                            self.checker.ctx.contextual_type = Some(asserted_type);
                        }

                        // Always type-check the expression for side effects / diagnostics.
                        let expr_type = self.checker.get_type_of_node(assertion.expression);

                        // Restore contextual type
                        self.checker.ctx.contextual_type = prev_contextual_type;
                        self.checker.ctx.in_const_assertion = prev_in_const_assertion;

                        if k == syntax_kind_ext::SATISFIES_EXPRESSION {
                            // `satisfies` keeps the expression type at runtime, but checks assignability.
                            // This is different from `as` which coerces the type.
                            self.checker.ensure_relation_input_ready(expr_type);
                            self.checker.ensure_relation_input_ready(asserted_type);
                            if !self.checker.type_contains_error(asserted_type) {
                                let _ = self.checker.check_satisfies_assignable_or_report(
                                    expr_type,
                                    asserted_type,
                                    assertion.expression,
                                );
                            }
                            expr_type
                        } else {
                            // `expr as T` / `<T>expr` yields `T`.
                            // TS2352: Check if conversion may be a mistake (types don't sufficiently overlap)
                            self.checker.ensure_relation_input_ready(expr_type);
                            self.checker.ensure_relation_input_ready(asserted_type);

                            // Don't check if either type is error, any, unknown, or never.
                            // TS also skips this warning for unconcretized type-parameter
                            // assertions like `{}` as T, which are common in generic code.
                            let should_check = !self.checker.type_contains_error(expr_type)
                                && !self.checker.type_contains_error(asserted_type)
                                && expr_type != TypeId::ANY
                                && asserted_type != TypeId::ANY
                                && expr_type != TypeId::UNKNOWN
                                && asserted_type != TypeId::UNKNOWN
                                && expr_type != TypeId::NEVER
                                && asserted_type != TypeId::NEVER
                                // Skip TS2352 when either type contains type parameters
                                // (not just *is* a type parameter). TSC's comparableRelation
                                // erases generics and resolves constraints, so assertions like
                                // `x as <T>(x: T) => T` or `key as keyof T` are allowed.
                                && !generic_query::contains_type_parameters(
                                    self.checker.ctx.types,
                                    expr_type,
                                )
                                && !generic_query::contains_type_parameters(
                                    self.checker.ctx.types,
                                    asserted_type,
                                );

                            if should_check {
                                // TS2352 is emitted if neither type is assignable to the other
                                // (i.e., the types don't "sufficiently overlap").
                                // TSC uses isTypeComparableTo which is more relaxed than assignability:
                                // types are comparable if they share at least one common property.
                                let source_to_target =
                                    self.checker.is_assignable_to(expr_type, asserted_type);
                                let target_to_source =
                                    self.checker.is_assignable_to(asserted_type, expr_type);
                                if !source_to_target && !target_to_source {
                                    // TSC uses isTypeComparableTo which decomposes unions
                                    // and checks per-member overlap. For `X as A | B`, it
                                    // suffices if X overlaps with ANY member (A or B).
                                    let mut have_overlap = false;

                                    // Decompose target union: any member assignable in either direction?
                                    if let Some(members) =
                                        query::union_members(self.checker.ctx.types, asserted_type)
                                    {
                                        for member in members {
                                            if self.checker.is_assignable_to(member, expr_type)
                                                || self.checker.is_assignable_to(expr_type, member)
                                            {
                                                have_overlap = true;
                                                break;
                                            }
                                        }
                                    }

                                    // Decompose source union: any member assignable in either direction?
                                    if !have_overlap
                                        && let Some(members) =
                                            query::union_members(self.checker.ctx.types, expr_type)
                                    {
                                        for member in members {
                                            if self.checker.is_assignable_to(member, asserted_type)
                                                || self
                                                    .checker
                                                    .is_assignable_to(asserted_type, member)
                                            {
                                                have_overlap = true;
                                                break;
                                            }
                                        }
                                    }

                                    // Final fallback: check structural property overlap
                                    if !have_overlap {
                                        let evaluated_expr =
                                            self.checker.evaluate_type_for_assignability(expr_type);
                                        let evaluated_asserted = self
                                            .checker
                                            .evaluate_type_for_assignability(asserted_type);
                                        have_overlap = query::types_are_comparable(
                                            self.checker.ctx.types,
                                            evaluated_expr,
                                            evaluated_asserted,
                                        );
                                    }

                                    if !have_overlap {
                                        self.checker.error_type_assertion_no_overlap(
                                            expr_type,
                                            asserted_type,
                                            idx,
                                        );
                                    }
                                }
                            }

                            asserted_type
                        }
                    }
                } else {
                    TypeId::ERROR
                }
            }

            // Template expression (e.g., `hello ${name}`)
            k if k == syntax_kind_ext::TEMPLATE_EXPRESSION => {
                self.checker.get_type_of_template_expression(idx)
            }

            // No-substitution template literal - always preserve literal type.
            // Widening happens at binding sites, not at expression evaluation.
            k if k == SyntaxKind::NoSubstitutionTemplateLiteral as u16 => self.resolve_literal(
                self.checker.literal_type_from_initializer(idx),
                TypeId::STRING,
            ),

            // =========================================================================
            // Type Nodes - Delegate to TypeNodeChecker
            // =========================================================================

            // Type nodes that need binder resolution - delegate to get_type_from_type_node
            // which handles special cases with proper symbol resolution
            k if k == syntax_kind_ext::TYPE_REFERENCE => self.checker.get_type_from_type_node(idx),

            // Type nodes handled by TypeNodeChecker
            k if k == syntax_kind_ext::UNION_TYPE
                || k == syntax_kind_ext::INTERSECTION_TYPE
                || k == syntax_kind_ext::ARRAY_TYPE
                || k == syntax_kind_ext::FUNCTION_TYPE
                || k == syntax_kind_ext::CONSTRUCTOR_TYPE
                || k == syntax_kind_ext::TYPE_LITERAL
                || k == syntax_kind_ext::TYPE_QUERY
                || k == syntax_kind_ext::TYPE_OPERATOR =>
            {
                let mut checker = crate::TypeNodeChecker::new(&mut self.checker.ctx);
                checker.check(idx)
            }

            // Keyword types - when recovered into value positions, TypeScript emits TS2693.
            // NullKeyword has no value-position check (null is a valid value).
            k if k == SyntaxKind::NullKeyword as u16 => TypeId::NULL,
            k if keyword_type_mapping(k).is_some() => {
                let (name, type_id) = keyword_type_mapping(k).unwrap();
                if self.checker.is_keyword_type_used_as_value_position(idx) {
                    self.checker.error_type_only_value_at(name, idx);
                    TypeId::ERROR
                } else {
                    type_id
                }
            }

            // Qualified name (A.B.C) - resolve namespace member access
            k if k == syntax_kind_ext::QUALIFIED_NAME => self.checker.resolve_qualified_name(idx),

            // Declaration nodes - not expressions, return VOID to avoid wasted work.
            // These are handled by check_statement → check_interface_declaration / check_class_declaration.
            // get_type_of_node may be called on them (e.g., for index signature compatibility checks),
            // but they don't have a meaningful expression type.
            k if k == syntax_kind_ext::INTERFACE_DECLARATION
                || k == syntax_kind_ext::CLASS_DECLARATION
                || k == syntax_kind_ext::TYPE_ALIAS_DECLARATION
                || k == syntax_kind_ext::ENUM_DECLARATION
                || k == syntax_kind_ext::MODULE_DECLARATION =>
            {
                TypeId::VOID
            }

            // JSX Elements (Rule #36: JSX Intrinsic Lookup)
            k if k == syntax_kind_ext::JSX_ELEMENT => {
                if let Some(jsx) = self.checker.ctx.arena.get_jsx_element(node) {
                    for &child in &jsx.children.nodes {
                        self.checker.get_type_of_node(child);
                    }
                    self.checker
                        .get_type_of_jsx_opening_element(jsx.opening_element)
                } else {
                    TypeId::ERROR
                }
            }
            k if k == syntax_kind_ext::JSX_SELF_CLOSING_ELEMENT => {
                self.checker.get_type_of_jsx_opening_element(idx)
            }
            k if k == syntax_kind_ext::JSX_FRAGMENT => {
                if let Some(jsx) = self.checker.ctx.arena.get_jsx_fragment(node) {
                    for &child in &jsx.children.nodes {
                        self.checker.get_type_of_node(child);
                    }
                }
                // JSX fragments resolve to JSX.Element type
                self.checker.get_jsx_element_type(idx)
            }
            k if k == syntax_kind_ext::JSX_EXPRESSION => {
                if let Some(jsx_expr) = self.checker.ctx.arena.get_jsx_expression(node) {
                    if jsx_expr.expression.is_some() {
                        self.checker.get_type_of_node(jsx_expr.expression)
                    } else {
                        TypeId::ANY
                    }
                } else {
                    TypeId::ERROR
                }
            }
            k if k == tsz_scanner::SyntaxKind::JsxText as u16 => TypeId::STRING,

            // Non-null assertion: x!
            k if k == syntax_kind_ext::NON_NULL_EXPRESSION => {
                // TS8013: Non-null assertions can only be used in TypeScript files
                if self.checker.is_js_file() {
                    use crate::diagnostics::{diagnostic_codes, diagnostic_messages};
                    self.checker.error_at_node(
                        idx,
                        diagnostic_messages::NON_NULL_ASSERTIONS_CAN_ONLY_BE_USED_IN_TYPESCRIPT_FILES,
                        diagnostic_codes::NON_NULL_ASSERTIONS_CAN_ONLY_BE_USED_IN_TYPESCRIPT_FILES,
                    );
                }
                // Get the operand type (strip the ! assertion — removes null/undefined)
                if let Some(unary) = self.checker.ctx.arena.get_unary_expr_ex(node) {
                    let operand_type = self.checker.get_type_of_node(unary.expression);
                    tsz_solver::remove_nullish(
                        self.checker.ctx.types.as_type_database(),
                        operand_type,
                    )
                } else {
                    TypeId::ERROR
                }
            }

            // Type predicate nodes appear in function return type positions
            // (`x is T` or `asserts x is T`). We delegate to type node resolution
            // to correctly get `boolean` or `void`.
            k if k == syntax_kind_ext::TYPE_PREDICATE => self.checker.get_type_from_type_node(idx),

            // ExpressionWithTypeArguments: `expr<T>` used as a standalone expression
            // (e.g., `List<number>.makeChild()`). Evaluate the inner expression
            // to trigger name resolution (TS2304) even though the overall node
            // produces a parse error (TS1477).
            k if k == syntax_kind_ext::EXPRESSION_WITH_TYPE_ARGUMENTS => {
                if let Some(data) = self.checker.ctx.arena.get_expr_type_args(node) {
                    self.checker.get_type_of_node(data.expression)
                } else {
                    TypeId::ERROR
                }
            }

            // Default case - unknown node kind is an error
            _ => {
                tracing::warn!(
                    idx = idx.0,
                    kind = node.kind,
                    "dispatch_type_computation: unknown expression kind"
                );
                TypeId::ERROR
            }
        }
    }
}

/// Maps a syntax kind to its keyword type name and `TypeId`.
///
/// Returns `Some((name, type_id))` for keyword types that need value-position
/// checking (TS2693), or `None` for non-keyword kinds.
/// `NullKeyword` is excluded because `null` is a valid value expression.
const fn keyword_type_mapping(kind: u16) -> Option<(&'static str, TypeId)> {
    match kind {
        k if k == SyntaxKind::NumberKeyword as u16 => Some(("number", TypeId::NUMBER)),
        k if k == SyntaxKind::StringKeyword as u16 => Some(("string", TypeId::STRING)),
        k if k == SyntaxKind::BooleanKeyword as u16 => Some(("boolean", TypeId::BOOLEAN)),
        k if k == SyntaxKind::VoidKeyword as u16 => Some(("void", TypeId::VOID)),
        k if k == SyntaxKind::AnyKeyword as u16 => Some(("any", TypeId::ANY)),
        k if k == SyntaxKind::NeverKeyword as u16 => Some(("never", TypeId::NEVER)),
        k if k == SyntaxKind::UnknownKeyword as u16 => Some(("unknown", TypeId::UNKNOWN)),
        k if k == SyntaxKind::UndefinedKeyword as u16 => Some(("undefined", TypeId::UNDEFINED)),
        k if k == SyntaxKind::ObjectKeyword as u16 => Some(("object", TypeId::OBJECT)),
        k if k == SyntaxKind::BigIntKeyword as u16 => Some(("bigint", TypeId::BIGINT)),
        k if k == SyntaxKind::SymbolKeyword as u16 => Some(("symbol", TypeId::SYMBOL)),
        _ => None,
    }
}