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
//! Assignment expression checking (simple, compound, logical, readonly).

use crate::diagnostics::{Diagnostic, diagnostic_codes, diagnostic_messages, format_message};
use crate::state::CheckerState;
use tsz_binder::symbol_flags;
use tsz_parser::parser::NodeIndex;
use tsz_parser::parser::flags::node_flags;
use tsz_parser::parser::syntax_kind_ext;
use tsz_scanner::SyntaxKind;
use tsz_solver::TypeId;

// =============================================================================
// Assignment Checking Methods
// =============================================================================

impl<'a> CheckerState<'a> {
    // =========================================================================
    // Assignment Operator Utilities
    // =========================================================================

    /// Check if a token is an assignment operator (=, +=, -=, etc.)
    pub(crate) const fn is_assignment_operator(&self, operator: u16) -> bool {
        matches!(
            operator,
            k if k == SyntaxKind::EqualsToken as u16
                || k == SyntaxKind::PlusEqualsToken as u16
                || k == SyntaxKind::MinusEqualsToken as u16
                || k == SyntaxKind::AsteriskEqualsToken as u16
                || k == SyntaxKind::AsteriskAsteriskEqualsToken as u16
                || k == SyntaxKind::SlashEqualsToken as u16
                || k == SyntaxKind::PercentEqualsToken as u16
                || k == SyntaxKind::LessThanLessThanEqualsToken as u16
                || k == SyntaxKind::GreaterThanGreaterThanEqualsToken as u16
                || k == SyntaxKind::GreaterThanGreaterThanGreaterThanEqualsToken as u16
                || k == SyntaxKind::AmpersandEqualsToken as u16
                || k == SyntaxKind::BarEqualsToken as u16
                || k == SyntaxKind::BarBarEqualsToken as u16
                || k == SyntaxKind::AmpersandAmpersandEqualsToken as u16
                || k == SyntaxKind::QuestionQuestionEqualsToken as u16
                || k == SyntaxKind::CaretEqualsToken as u16
        )
    }

    // =========================================================================
    // Assignment Expression Checking
    // =========================================================================

    /// Check if a node is a valid assignment target (variable, property access, element access,
    /// or destructuring pattern).
    ///
    /// Returns false for literals, call expressions, and other non-assignable expressions.
    /// Used to emit TS2364: "The left-hand side of an assignment expression must be a variable
    /// or a property access."
    pub(crate) fn is_valid_assignment_target(&self, idx: NodeIndex) -> bool {
        use tsz_parser::parser::syntax_kind_ext;
        let Some(node) = self.ctx.arena.get(idx) else {
            return false;
        };
        match node.kind {
            k if k == SyntaxKind::Identifier as u16 => true,
            k if k == syntax_kind_ext::PROPERTY_ACCESS_EXPRESSION
                || k == syntax_kind_ext::ELEMENT_ACCESS_EXPRESSION =>
            {
                true
            }
            k if k == syntax_kind_ext::OBJECT_BINDING_PATTERN
                || k == syntax_kind_ext::ARRAY_BINDING_PATTERN
                || k == syntax_kind_ext::OBJECT_LITERAL_EXPRESSION
                || k == syntax_kind_ext::ARRAY_LITERAL_EXPRESSION =>
            {
                true
            }
            k if k == syntax_kind_ext::PARENTHESIZED_EXPRESSION => {
                // Check the inner expression
                if let Some(paren) = self.ctx.arena.get_parenthesized(node) {
                    self.is_valid_assignment_target(paren.expression)
                } else {
                    false
                }
            }
            k if k == syntax_kind_ext::SATISFIES_EXPRESSION
                || k == syntax_kind_ext::AS_EXPRESSION =>
            {
                // Satisfies and as expressions are valid assignment targets if their inner expression is valid
                // Example: (x satisfies number) = 10
                if let Some(assertion) = self.ctx.arena.get_type_assertion(node) {
                    self.is_valid_assignment_target(assertion.expression)
                } else {
                    false
                }
            }
            _ => false,
        }
    }

    /// Check if an identifier node refers to a const variable.
    ///
    /// Returns `Some(name)` if the identifier refers to a const, `None` otherwise.
    fn get_const_variable_name(&self, ident_idx: NodeIndex) -> Option<String> {
        let ident_idx = self.unwrap_assignment_target_for_symbol(ident_idx);
        let node = self.ctx.arena.get(ident_idx)?;
        if node.kind != SyntaxKind::Identifier as u16 {
            return None;
        }
        let ident = self.ctx.arena.get_identifier(node)?;
        let name = ident.escaped_text.clone();

        // Use binder-level resolution (no tracking side-effect) to avoid marking
        // the assignment target as "read" in `referenced_symbols`. The const check
        // is a read-only query — assignment targets should only be tracked via
        // `resolve_identifier_symbol_for_write` in `get_type_of_assignment_target`.
        // Using the tracking `resolve_identifier_symbol` here would suppress TS6133
        // for write-only parameters (e.g., `person2 = "dummy value"` should still
        // flag `person2` as unused).
        let sym_id = self
            .ctx
            .binder
            .resolve_identifier(self.ctx.arena, ident_idx)?;

        // Find the correct binder and arena for this symbol
        let mut target_binder = self.ctx.binder;
        let mut target_arena = self.ctx.arena;

        if let Some(&file_idx) = self.ctx.cross_file_symbol_targets.borrow().get(&sym_id) {
            if let Some(all_binders) = &self.ctx.all_binders
                && let Some(b) = all_binders.get(file_idx)
            {
                target_binder = b;
            }
            if let Some(all_arenas) = &self.ctx.all_arenas
                && let Some(a) = all_arenas.get(file_idx)
            {
                target_arena = a;
            }
        } else if let Some(arena) = self.ctx.binder.symbol_arenas.get(&sym_id) {
            // It could be a lib symbol where target_binder is still self.ctx.binder (due to merging)
            // or one of the lib_contexts.
            target_arena = arena.as_ref();
        }

        // Also check if it's from a lib context
        for lib in &self.ctx.lib_contexts {
            if let Some(sym) = lib.binder.get_symbol(sym_id)
                && sym.escaped_name == name
            {
                target_binder = &lib.binder;
                target_arena = lib.arena.as_ref();
                break;
            }
        }

        let symbol = target_binder
            .get_symbol(sym_id)
            .or_else(|| self.ctx.binder.get_symbol(sym_id))?;
        let value_decl = symbol.value_declaration;
        if value_decl.is_none() {
            return None;
        }

        // Sometimes the declaration is specifically registered in declaration_arenas
        if let Some(arenas) = self
            .ctx
            .binder
            .declaration_arenas
            .get(&(sym_id, value_decl))
            && let Some(first) = arenas.first()
        {
            target_arena = first.as_ref();
        }

        let decl_node = target_arena.get(value_decl)?;
        let mut decl_flags = decl_node.flags as u32;

        // If CONST/LET not directly on node, check parent (VariableDeclarationList)
        if (decl_flags & (node_flags::LET | node_flags::CONST)) == 0
            && let Some(ext) = target_arena.get_extended(value_decl)
            && let Some(parent_node) = target_arena.get(ext.parent)
            && parent_node.kind == syntax_kind_ext::VARIABLE_DECLARATION_LIST
        {
            decl_flags |= parent_node.flags as u32;
        }

        (decl_flags & node_flags::CONST != 0).then_some(name)
    }

    /// Emit TS1100 when assigning to strict-mode reserved identifiers.
    ///
    /// `arguments` and `eval` are disallowed in strict mode for assignments.
    /// This mirrors existing declaration-site checks and keeps diagnostics in
    /// parity with TypeScript's behavior for strict-mode built-ins.
    fn check_strict_assignment_target(&mut self, target_idx: NodeIndex) {
        let inner = self.skip_parenthesized_expression(target_idx);
        if !self.is_strict_mode_for_node(inner) {
            return;
        }
        let Some(node) = self.ctx.arena.get(inner) else {
            return;
        };
        if node.kind != SyntaxKind::Identifier as u16 {
            return;
        }

        let Some(id_data) = self.ctx.arena.get_identifier(node) else {
            return;
        };
        if id_data.escaped_text != "arguments" && id_data.escaped_text != "eval" {
            return;
        }

        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(inner, code, &[&id_data.escaped_text]);
    }

    /// Strip wrappers that preserve assignment target identity for symbol checks.
    ///
    /// Examples:
    /// - `(x)` -> `x`
    /// - `x!` -> `x`
    /// - `(x as T)` -> `x`
    /// - `(x satisfies T)` -> `x`
    fn unwrap_assignment_target_for_symbol(&self, idx: NodeIndex) -> NodeIndex {
        self.ctx.arena.skip_parenthesized_and_assertions(idx)
    }

    /// Check if the operand of an increment/decrement operator is a valid l-value (TS2357).
    ///
    /// The operand must be a variable (Identifier), property access, or element access.
    /// Expressions like `(1 + 2)++` or `1++` are not valid.
    /// Transparent wrappers are skipped: parenthesized, non-null assertion, type assertion,
    /// and satisfies expressions (e.g., `foo[x]!++` and `(a satisfies number)++` are valid).
    /// Returns `true` if an error was emitted.
    pub(crate) fn check_increment_decrement_operand(&mut self, operand_idx: NodeIndex) -> bool {
        let inner = self.skip_assignment_transparent_wrappers(operand_idx);
        let Some(node) = self.ctx.arena.get(inner) else {
            return false;
        };

        let is_valid = node.kind == SyntaxKind::Identifier as u16
            || node.kind == syntax_kind_ext::PROPERTY_ACCESS_EXPRESSION
            || node.kind == syntax_kind_ext::ELEMENT_ACCESS_EXPRESSION;

        if !is_valid {
            self.error_at_node(
                operand_idx,
                diagnostic_messages::THE_OPERAND_OF_AN_INCREMENT_OR_DECREMENT_OPERATOR_MUST_BE_A_VARIABLE_OR_A_PROPER,
                diagnostic_codes::THE_OPERAND_OF_AN_INCREMENT_OR_DECREMENT_OPERATOR_MUST_BE_A_VARIABLE_OR_A_PROPER,
            );
            return true;
        }

        false
    }

    /// Skip through transparent wrapper expressions that don't affect l-value validity.
    ///
    /// Skips: parenthesized, non-null assertion (`!`), type assertion (`as`/angle-bracket),
    /// and `satisfies` expressions.
    fn skip_assignment_transparent_wrappers(&self, idx: NodeIndex) -> NodeIndex {
        self.ctx.arena.skip_parenthesized_and_assertions(idx)
    }

    /// Check if the assignment target (LHS) is a const variable and emit TS2588 if so.
    ///
    /// Resolves through parenthesized expressions to find the underlying identifier.
    /// Returns `true` if a TS2588 error was emitted (caller should skip further type checks).
    pub(crate) fn check_const_assignment(&mut self, target_idx: NodeIndex) -> bool {
        let inner = self.skip_parenthesized_expression(target_idx);
        if let Some(name) = self.get_const_variable_name(inner) {
            self.error_at_node_msg(
                inner,
                diagnostic_codes::CANNOT_ASSIGN_TO_BECAUSE_IT_IS_A_CONSTANT,
                &[&name],
            );
            return true;
        }
        false
    }

    /// Check if assignment target is a function and emit TS2630 error.
    ///
    /// TypeScript does not allow direct assignment to functions:
    /// ```typescript
    /// function foo() {}
    /// foo = bar;  // Error TS2630: Cannot assign to 'foo' because it is a function.
    /// ```
    ///
    /// Also checks for built-in global functions (eval, arguments) which always
    /// emit TS2630 when assigned to, even without explicit function declarations.
    ///
    /// This check helps catch common mistakes where users try to reassign function names.
    pub(crate) fn check_function_assignment(&mut self, target_idx: NodeIndex) -> bool {
        let inner = self.skip_parenthesized_expression(target_idx);

        // Only check identifiers - property access like obj.fn = x is allowed
        let Some(node) = self.ctx.arena.get(inner) else {
            return false;
        };
        if node.kind != SyntaxKind::Identifier as u16 {
            return false;
        }

        // Get the identifier name
        let Some(id_data) = self.ctx.arena.get_identifier(node) else {
            return false;
        };
        let name = &id_data.escaped_text;

        // `undefined` is not a variable — it's a global constant that cannot be assigned to.
        // TypeScript emits TS2539 for `undefined = ...` or `undefined++` etc.
        if name == "undefined" {
            let message = format_message(
                diagnostic_messages::CANNOT_ASSIGN_TO_BECAUSE_IT_IS_NOT_A_VARIABLE,
                &[name],
            );
            self.ctx.diagnostics.push(Diagnostic::error(
                self.ctx.file_name.clone(),
                node.pos,
                node.end.saturating_sub(node.pos),
                message,
                diagnostic_codes::CANNOT_ASSIGN_TO_BECAUSE_IT_IS_NOT_A_VARIABLE,
            ));
            return true;
        }

        // Check for built-in global functions that always error with TS2630
        // Note: `arguments` is NOT included here because inside function bodies,
        // `arguments` is an IArguments object (handled by type_computation_complex.rs).
        // Only at module scope would `arguments` resolve to a function-like global.
        if name == "eval" {
            use crate::diagnostics::{diagnostic_codes, diagnostic_messages, format_message};
            let message = format_message(
                diagnostic_messages::CANNOT_ASSIGN_TO_BECAUSE_IT_IS_A_FUNCTION,
                &[name],
            );
            self.ctx.diagnostics.push(Diagnostic::error(
                self.ctx.file_name.clone(),
                node.pos,
                node.end.saturating_sub(node.pos),
                message,
                diagnostic_codes::CANNOT_ASSIGN_TO_BECAUSE_IT_IS_A_FUNCTION,
            ));
            return true;
        }

        // Look up the symbol for this identifier by resolving it through the scope chain
        // Note: We use resolve_identifier instead of node_symbols because node_symbols
        // only contains declaration nodes, not identifier references.
        let sym_id = self.ctx.binder.resolve_identifier(self.ctx.arena, inner);
        let Some(sym_id) = sym_id else {
            return false;
        };

        let Some(symbol) = self.ctx.binder.get_symbol(sym_id) else {
            return false;
        };

        // Check for uninstantiated namespaces first (TS2708)
        let is_namespace = (symbol.flags & symbol_flags::NAMESPACE_MODULE) != 0;
        let value_flags_except_module = symbol_flags::VALUE & !symbol_flags::VALUE_MODULE;
        let has_other_value = (symbol.flags & value_flags_except_module) != 0;

        if is_namespace && !has_other_value {
            let mut is_instantiated = false;
            for decl_idx in &symbol.declarations {
                if self.is_namespace_declaration_instantiated(*decl_idx) {
                    is_instantiated = true;
                    break;
                }
            }
            if !is_instantiated {
                self.error_namespace_used_as_value_at(name, inner);
                return true;
            }
        }

        // Check for type-only symbols used as values in assignment position (TS2693)
        if symbol.flags & symbol_flags::TYPE != 0 && symbol.flags & symbol_flags::VALUE == 0 {
            self.error_type_only_value_at(name, inner);
            return true;
        }

        // Check if this symbol is a class, enum, function, or namespace (TS2629, TS2628, TS2630, TS2631)
        use crate::diagnostics::{diagnostic_codes, diagnostic_messages, format_message};
        let (msg_template, code) = if symbol.flags & symbol_flags::MODULE != 0 {
            (
                diagnostic_messages::CANNOT_ASSIGN_TO_BECAUSE_IT_IS_A_NAMESPACE,
                diagnostic_codes::CANNOT_ASSIGN_TO_BECAUSE_IT_IS_A_NAMESPACE,
            )
        } else if symbol.flags & symbol_flags::CLASS != 0 {
            (
                diagnostic_messages::CANNOT_ASSIGN_TO_BECAUSE_IT_IS_A_CLASS,
                diagnostic_codes::CANNOT_ASSIGN_TO_BECAUSE_IT_IS_A_CLASS,
            )
        } else if symbol.flags & symbol_flags::ENUM != 0 {
            (
                diagnostic_messages::CANNOT_ASSIGN_TO_BECAUSE_IT_IS_AN_ENUM,
                diagnostic_codes::CANNOT_ASSIGN_TO_BECAUSE_IT_IS_AN_ENUM,
            )
        } else if symbol.flags & symbol_flags::FUNCTION != 0 {
            (
                diagnostic_messages::CANNOT_ASSIGN_TO_BECAUSE_IT_IS_A_FUNCTION,
                diagnostic_codes::CANNOT_ASSIGN_TO_BECAUSE_IT_IS_A_FUNCTION,
            )
        } else {
            return false;
        };

        let message = format_message(msg_template, &[name]);
        self.ctx.diagnostics.push(Diagnostic::error(
            self.ctx.file_name.clone(),
            node.pos,
            node.end.saturating_sub(node.pos),
            message,
            code,
        ));
        true
    }

    /// Check an assignment expression (=).
    ///
    /// ## Contextual Typing:
    /// - The LHS type is used as contextual type for the RHS expression
    /// - This enables better type inference for object literals, etc.
    ///
    /// ## Validation:
    /// - Checks constructor accessibility (if applicable)
    /// - Validates that RHS is assignable to LHS
    /// - Checks for excess properties in object literals
    /// - Validates readonly assignments
    pub(crate) fn check_assignment_expression(
        &mut self,
        left_idx: NodeIndex,
        right_idx: NodeIndex,
        expr_idx: NodeIndex,
    ) -> TypeId {
        // TS2364: The left-hand side of an assignment expression must be a variable or a property access.
        // Suppress when the LHS is near a parse error (e.g. `1 >>/**/= 2;` where `>>=` is split
        // by a comment — the parser already emits TS1109 and the assignment is a recovery artifact).
        if !self.is_valid_assignment_target(left_idx) && !self.node_has_nearby_parse_error(left_idx)
        {
            self.error_at_node(
                left_idx,
                "The left-hand side of an assignment expression must be a variable or a property access.",
                diagnostic_codes::THE_LEFT_HAND_SIDE_OF_AN_ASSIGNMENT_EXPRESSION_MUST_BE_A_VARIABLE_OR_A_PROPERTY,
            );
            self.get_type_of_node(left_idx);
            self.get_type_of_node(right_idx);
            return TypeId::ANY;
        }

        // TS2588: Cannot assign to 'x' because it is a constant.
        // Check early - if this fires, skip type assignability checks (tsc behavior).
        let is_const = self.check_const_assignment(left_idx);

        // TS2630: Cannot assign to 'x' because it is a function.
        // This check must come after valid assignment target check but before type checking.
        let is_function_assignment = self.check_function_assignment(left_idx);

        self.check_strict_assignment_target(left_idx);

        // Set destructuring flag when LHS is an object/array pattern to suppress
        // TS1117 (duplicate property) checks in destructuring targets.
        let (is_destructuring, is_array_destructuring) =
            if let Some(left_node) = self.ctx.arena.get(left_idx) {
                let is_obj = left_node.kind == syntax_kind_ext::OBJECT_LITERAL_EXPRESSION;
                let is_arr = left_node.kind == syntax_kind_ext::ARRAY_LITERAL_EXPRESSION;
                (is_obj || is_arr, is_arr)
            } else {
                (false, false)
            };
        let prev_destructuring = self.ctx.in_destructuring_target;
        if is_destructuring {
            self.ctx.in_destructuring_target = true;
        }
        let left_target = self.get_type_of_assignment_target(left_idx);
        self.ctx.in_destructuring_target = prev_destructuring;
        let mut left_type = self.resolve_type_query_type(left_target);

        // In JS/checkJs mode, allow JSDoc `@type` on assignment statements to
        // provide the contextual target type for the LHS.
        //
        // Example:
        //   /** @type {string} */
        //   C.prototype = 12;
        let is_js_file = self.ctx.file_name.ends_with(".js")
            || self.ctx.file_name.ends_with(".jsx")
            || self.ctx.file_name.ends_with(".mjs")
            || self.ctx.file_name.ends_with(".cjs");
        if is_js_file
            && self.ctx.compiler_options.check_js
            && let Some(jsdoc_left_type) = self
                .jsdoc_type_annotation_for_node_direct(expr_idx)
                .or_else(|| self.jsdoc_type_annotation_for_node_direct(left_idx))
        {
            left_type = jsdoc_left_type;
        }

        let prev_context = self.ctx.contextual_type;
        if left_type != TypeId::ANY
            && left_type != TypeId::NEVER
            && left_type != TypeId::UNKNOWN
            && !self.type_contains_error(left_type)
        {
            self.ctx.contextual_type = Some(left_type);
        }

        let right_raw = self.get_type_of_node(right_idx);
        let right_type = self.resolve_type_query_type(right_raw);

        // NOTE: Freshness is now tracked on the TypeId via ObjectFlags.
        // No need to manually track freshness removal here.

        self.ctx.contextual_type = prev_context;

        if is_function_assignment {
            // TS2630 is terminal in TypeScript for simple assignment targets.
            // Avoid cascading TS2322/other assignability diagnostics.
            return right_type;
        }

        self.ensure_relation_input_ready(right_type);
        self.ensure_relation_input_ready(left_type);

        if is_array_destructuring {
            // TS2488: Array destructuring assignments require an iterable RHS.
            // Keep parity with `[] = value` behavior by skipping empty patterns.
            let should_check_iterability = self
                .ctx
                .arena
                .get(left_idx)
                .and_then(|node| self.ctx.arena.get_literal_expr(node))
                .is_none_or(|array_lit| !array_lit.elements.nodes.is_empty());
            if should_check_iterability {
                self.check_destructuring_iterability(left_idx, right_type, NodeIndex::NONE);
            }
            self.check_tuple_destructuring_bounds(left_idx, right_type);
        }

        let is_readonly = if !is_const {
            self.check_readonly_assignment(left_idx, expr_idx)
        } else {
            false
        };

        let blocked_generic_index_write =
            !is_const && !is_readonly && self.check_generic_indexed_write_restriction(left_idx);

        if !is_const && !is_readonly && !blocked_generic_index_write && left_type != TypeId::ANY {
            let mut check_assignability = !is_array_destructuring;

            if check_assignability {
                let widened_left = tsz_solver::widening::widen_type(self.ctx.types, left_type);
                if widened_left != left_type
                    && let Some(right_node) = self.ctx.arena.get(right_idx)
                {
                    use tsz_parser::parser::syntax_kind_ext;
                    use tsz_scanner::SyntaxKind;
                    if right_node.kind == syntax_kind_ext::BINARY_EXPRESSION
                        && let Some(bin) = self.ctx.arena.get_binary_expr(right_node)
                    {
                        let op = bin.operator_token;
                        let is_compound_like = op == SyntaxKind::PlusToken as u16
                            || op == SyntaxKind::MinusToken as u16
                            || op == SyntaxKind::AsteriskToken as u16
                            || op == SyntaxKind::SlashToken as u16
                            || op == SyntaxKind::PercentToken as u16
                            || op == SyntaxKind::AsteriskAsteriskToken as u16
                            || op == SyntaxKind::LessThanLessThanToken as u16
                            || op == SyntaxKind::GreaterThanGreaterThanToken as u16
                            || op == SyntaxKind::GreaterThanGreaterThanGreaterThanToken as u16;

                        if is_compound_like && self.is_assignable_to(right_type, widened_left) {
                            check_assignability = false;
                        }
                    }
                }
            }

            self.check_assignment_compatibility(
                left_idx,
                right_idx,
                right_type,
                left_type,
                check_assignability, // check_assignability
                true,
            );

            if left_type != TypeId::UNKNOWN
                && let Some(right_node) = self.ctx.arena.get(right_idx)
                && right_node.kind == syntax_kind_ext::OBJECT_LITERAL_EXPRESSION
            {
                self.check_object_literal_excess_properties(right_type, left_type, right_idx);
            }
        }

        right_type
    }

    fn check_generic_indexed_write_restriction(&mut self, left_idx: NodeIndex) -> bool {
        let Some(left_node) = self.ctx.arena.get(left_idx) else {
            return false;
        };
        if left_node.kind != syntax_kind_ext::ELEMENT_ACCESS_EXPRESSION {
            return false;
        }
        let Some(access) = self.ctx.arena.get_access_expr(left_node) else {
            return false;
        };
        if self
            .get_literal_index_from_node(access.name_or_argument)
            .is_some()
        {
            return false;
        }

        let prev_skip_narrowing = self.ctx.skip_flow_narrowing;
        self.ctx.skip_flow_narrowing = true;
        let object_type_raw = self.get_type_of_node(access.expression);
        let object_type = self.resolve_type_query_type(object_type_raw);
        self.ctx.skip_flow_narrowing = prev_skip_narrowing;

        if object_type == TypeId::ERROR || object_type == TypeId::ANY {
            return false;
        }
        if !tsz_solver::type_queries::contains_type_parameters_db(self.ctx.types, object_type) {
            return false;
        }

        let evaluated_object = tsz_solver::evaluate_type(self.ctx.types, object_type);

        // In TypeScript, assigning to a generic mapped type (like `Record<K, V>`) is safe
        // if the index is constrained correctly, and the solver handles the assignability.
        // We skip the generic write restriction for mapped types (and applications of aliases
        // that aren't pure type parameters) so it falls through to normal property checking.

        // If it's an intersection or union, it might contain a type parameter.
        // Let's approximate TS2862 by only blocking if the evaluated type is an uninstantiated
        // type parameter directly, or an intersection of them.
        if !tsz_solver::type_queries::is_uninstantiated_type_parameter(
            self.ctx.types,
            evaluated_object,
        ) {
            return false;
        }

        if self.index_expression_constrained_to_object_keys(object_type, access.name_or_argument) {
            return false;
        }

        let object_type_str = self.format_type(object_type);
        let message = format_message(
            diagnostic_messages::TYPE_IS_GENERIC_AND_CAN_ONLY_BE_INDEXED_FOR_READING,
            &[&object_type_str],
        );
        self.error_at_node(
            left_idx,
            &message,
            diagnostic_codes::TYPE_IS_GENERIC_AND_CAN_ONLY_BE_INDEXED_FOR_READING,
        );
        true
    }

    fn index_expression_constrained_to_object_keys(
        &mut self,
        object_type: TypeId,
        index_expr: NodeIndex,
    ) -> bool {
        use tsz_solver::type_queries::{get_keyof_type, get_type_parameter_constraint};

        let index_type = self.get_type_of_node(index_expr);
        if index_type == TypeId::ERROR {
            return false;
        }

        let Some(index_constraint) = get_type_parameter_constraint(self.ctx.types, index_type)
        else {
            return false;
        };

        let Some(constraint_source) = get_keyof_type(self.ctx.types, index_constraint) else {
            return false;
        };

        // Allow indexed writes when the constraint is `keyof <source>` and
        // the source type is compatible with the generic object being written to.
        self.is_subtype_of(constraint_source, object_type)
            || self.is_subtype_of(object_type, constraint_source)
    }

    fn check_tuple_destructuring_bounds(&mut self, left_idx: NodeIndex, right_type: TypeId) {
        let rhs = tsz_solver::type_queries::unwrap_readonly(self.ctx.types, right_type);
        let Some(tuple_elements) =
            tsz_solver::type_queries::get_tuple_elements(self.ctx.types, rhs)
        else {
            return;
        };

        let has_rest_tail = tuple_elements.last().is_some_and(|element| element.rest);
        if has_rest_tail {
            return;
        }

        let Some(left_node) = self.ctx.arena.get(left_idx) else {
            return;
        };
        let Some(array_lit) = self.ctx.arena.get_literal_expr(left_node) else {
            return;
        };

        for (index, &element_idx) in array_lit.elements.nodes.iter().enumerate() {
            if index < tuple_elements.len() || element_idx.is_none() {
                continue;
            }
            let Some(element_node) = self.ctx.arena.get(element_idx) else {
                continue;
            };
            if element_node.kind == syntax_kind_ext::OMITTED_EXPRESSION {
                continue;
            }
            if element_node.kind == syntax_kind_ext::SPREAD_ELEMENT {
                return;
            }

            self.error_at_node(
                element_idx,
                &format!(
                    "Tuple type of length '{}' has no element at index '{}'.",
                    tuple_elements.len(),
                    index
                ),
                diagnostic_codes::TUPLE_TYPE_OF_LENGTH_HAS_NO_ELEMENT_AT_INDEX,
            );
            return;
        }
    }

    // =========================================================================
    // Arithmetic Operand Validation
    // =========================================================================

    /// Check if an operand type is valid for arithmetic operations.
    ///
    /// Returns true if the type is number, bigint, any, or an enum type.
    /// This is used to validate operands for TS2362/TS2363 errors.
    fn is_arithmetic_operand(&self, type_id: TypeId) -> bool {
        use tsz_solver::BinaryOpEvaluator;

        // Check if this is an enum type (Lazy/DefId to an enum symbol)
        if let Some(sym_id) = self.ctx.resolve_type_to_symbol_id(type_id)
            && let Some(symbol) = self.ctx.binder.get_symbol(sym_id)
        {
            // Check if the symbol is an enum (ENUM flags)
            use tsz_binder::symbol_flags;
            if (symbol.flags & symbol_flags::ENUM) != 0 {
                return true;
            }
        }

        let evaluator = BinaryOpEvaluator::new(self.ctx.types);
        evaluator.is_arithmetic_operand(type_id)
    }

    /// Check and emit TS2362/TS2363 errors for arithmetic operations.
    ///
    /// For operators like -, *, /, %, **, -=, *=, /=, %=, **=,
    /// validates that operands are of type number, bigint, any, or enum.
    /// Emits appropriate errors when operands are invalid.
    /// Returns true if any error was emitted.
    fn check_arithmetic_operands(
        &mut self,
        left_idx: NodeIndex,
        right_idx: NodeIndex,
        left_type: TypeId,
        right_type: TypeId,
    ) -> bool {
        // Evaluate types to resolve unevaluated conditional/mapped types before checking.
        // e.g. DeepPartial<number> (conditional: number extends object ? ... : number) → number
        let left_eval = self.evaluate_type_for_binary_ops(left_type);
        let right_eval = self.evaluate_type_for_binary_ops(right_type);
        let left_is_valid = self.is_arithmetic_operand(left_eval);
        let right_is_valid = self.is_arithmetic_operand(right_eval);

        if !left_is_valid && let Some(loc) = self.get_source_location(left_idx) {
            self.ctx.diagnostics.push(Diagnostic::error(self.ctx.file_name.clone(), loc.start, loc.length(), "The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.".to_string(), diagnostic_codes::THE_LEFT_HAND_SIDE_OF_AN_ARITHMETIC_OPERATION_MUST_BE_OF_TYPE_ANY_NUMBER_BIGINT));
        }

        if !right_is_valid && let Some(loc) = self.get_source_location(right_idx) {
            self.ctx.diagnostics.push(Diagnostic::error(self.ctx.file_name.clone(), loc.start, loc.length(), "The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.".to_string(), diagnostic_codes::THE_RIGHT_HAND_SIDE_OF_AN_ARITHMETIC_OPERATION_MUST_BE_OF_TYPE_ANY_NUMBER_BIGINT));
        }

        !left_is_valid || !right_is_valid
    }

    /// Emit TS2447 error for boolean bitwise operators (&, |, ^, &=, |=, ^=).
    fn emit_boolean_operator_error(&mut self, node_idx: NodeIndex, op_str: &str, suggestion: &str) {
        if let Some(loc) = self.get_source_location(node_idx) {
            let message = format!(
                "The '{op_str}' operator is not allowed for boolean types. Consider using '{suggestion}' instead."
            );
            self.ctx.diagnostics.push(Diagnostic::error(self.ctx.file_name.clone(), loc.start, loc.length(), message, diagnostic_codes::THE_OPERATOR_IS_NOT_ALLOWED_FOR_BOOLEAN_TYPES_CONSIDER_USING_INSTEAD));
        }
    }

    // =========================================================================
    // Compound Assignment Checking
    // =========================================================================

    /// Check a compound assignment expression (+=, &&=, ??=, etc.).
    ///
    /// Compound assignments have special type computation rules:
    /// - Logical assignments (&&=, ||=, ??=) assign the RHS type
    /// - Other compound assignments assign the computed result type
    ///
    /// ## Type Computation:
    /// - Numeric operators (+, -, *, /, %) compute number type
    /// - Bitwise operators compute number type
    /// - Logical operators return RHS type
    pub(crate) fn check_compound_assignment_expression(
        &mut self,
        left_idx: NodeIndex,
        right_idx: NodeIndex,
        operator: u16,
        expr_idx: NodeIndex,
    ) -> TypeId {
        // TS2364: The left-hand side of an assignment expression must be a variable or a property access.
        // Suppress when near a parse error (same rationale as in check_assignment_expression).
        if !self.is_valid_assignment_target(left_idx) && !self.node_has_nearby_parse_error(left_idx)
        {
            self.error_at_node(
                left_idx,
                "The left-hand side of an assignment expression must be a variable or a property access.",
                diagnostic_codes::THE_LEFT_HAND_SIDE_OF_AN_ASSIGNMENT_EXPRESSION_MUST_BE_A_VARIABLE_OR_A_PROPERTY,
            );
            self.get_type_of_node(left_idx);
            self.get_type_of_node(right_idx);
            return TypeId::ANY;
        }

        // TS2588: Cannot assign to 'x' because it is a constant.
        let is_const = self.check_const_assignment(left_idx);

        // TS2629/TS2628/TS2630: Cannot assign to class/enum/function.
        let is_function_assignment = self.check_function_assignment(left_idx);

        self.check_strict_assignment_target(left_idx);

        // Compound assignments read the LHS before writing, so the LHS identifier
        // must go through definite assignment analysis (TS2454). Without this,
        // `var x: number; x += 1;` would not trigger "used before assigned".
        if let Some(left_node) = self.ctx.arena.get(left_idx)
            && left_node.kind == SyntaxKind::Identifier as u16
            && let Some(sym_id) = self.resolve_identifier_symbol(left_idx)
        {
            let declared_type = self.get_type_of_symbol(sym_id);
            self.check_flow_usage(left_idx, declared_type, sym_id);
        }

        let left_target = self.get_type_of_assignment_target(left_idx);
        let left_type = self.resolve_type_query_type(left_target);

        let prev_context = self.ctx.contextual_type;
        if left_type != TypeId::ANY
            && left_type != TypeId::NEVER
            && left_type != TypeId::UNKNOWN
            && !self.type_contains_error(left_type)
        {
            self.ctx.contextual_type = Some(left_type);
        }

        let right_raw = self.get_type_of_node(right_idx);
        let right_type = self.resolve_type_query_type(right_raw);

        // NOTE: Freshness is now tracked on the TypeId via ObjectFlags.
        // No need to manually track freshness removal here.

        self.ctx.contextual_type = prev_context;

        self.ensure_relation_input_ready(right_type);
        self.ensure_relation_input_ready(left_type);

        let is_readonly = if !is_const {
            self.check_readonly_assignment(left_idx, expr_idx)
        } else {
            false
        };

        // Track whether an operator error was emitted so we can suppress cascading TS2322.
        // TSC doesn't emit TS2322 when there's already an operator error (TS2447/TS2362/TS2363).
        let mut emitted_operator_error = is_const || is_readonly || is_function_assignment;

        let op_str = match operator {
            k if k == SyntaxKind::PlusEqualsToken as u16 => "+",
            k if k == SyntaxKind::MinusEqualsToken as u16 => "-",
            k if k == SyntaxKind::AsteriskEqualsToken as u16 => "*",
            k if k == SyntaxKind::SlashEqualsToken as u16 => "/",
            k if k == SyntaxKind::PercentEqualsToken as u16 => "%",
            k if k == SyntaxKind::AsteriskAsteriskEqualsToken as u16 => "**",
            k if k == SyntaxKind::AmpersandEqualsToken as u16 => "&",
            k if k == SyntaxKind::BarEqualsToken as u16 => "|",
            k if k == SyntaxKind::CaretEqualsToken as u16 => "^",
            k if k == SyntaxKind::LessThanLessThanEqualsToken as u16 => "<<",
            k if k == SyntaxKind::GreaterThanGreaterThanEqualsToken as u16 => ">>",
            k if k == SyntaxKind::GreaterThanGreaterThanGreaterThanEqualsToken as u16 => ">>>",
            _ => "",
        };

        if !op_str.is_empty() {
            emitted_operator_error |= self.check_and_emit_nullish_binary_operands(
                left_idx, right_idx, left_type, right_type, op_str,
            );
        }

        // Check arithmetic operands for compound arithmetic assignments
        // Emit TS2362/TS2363 for -=, *=, /=, %=, **=
        let is_arithmetic_compound = matches!(
            operator,
            k if k == SyntaxKind::MinusEqualsToken as u16
                || k == SyntaxKind::AsteriskEqualsToken as u16
                || k == SyntaxKind::SlashEqualsToken as u16
                || k == SyntaxKind::PercentEqualsToken as u16
                || k == SyntaxKind::AsteriskAsteriskEqualsToken as u16
        );
        if is_arithmetic_compound && !is_function_assignment {
            // Don't emit arithmetic errors if either operand is ERROR - prevents cascading errors
            if left_type != TypeId::ERROR && right_type != TypeId::ERROR {
                emitted_operator_error |=
                    self.check_arithmetic_operands(left_idx, right_idx, left_type, right_type);
            }
        }

        // TS2791: bigint exponentiation assignment requires target >= ES2016.
        // Skip when either type is any/unknown (TSC skips the bigint branch for those).
        if operator == SyntaxKind::AsteriskAsteriskEqualsToken as u16
            && (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(
                expr_idx,
                crate::diagnostics::diagnostic_codes::EXPONENTIATION_CANNOT_BE_PERFORMED_ON_BIGINT_VALUES_UNLESS_THE_TARGET_OPTION_IS,
                &[],
            );
            emitted_operator_error = true;
        }

        // Check bitwise compound assignments: &=, |=, ^=, <<=, >>=, >>>=
        let is_boolean_bitwise_compound = matches!(
            operator,
            k if k == SyntaxKind::AmpersandEqualsToken as u16
                || k == SyntaxKind::BarEqualsToken as u16
                || k == SyntaxKind::CaretEqualsToken as u16
        );
        let is_shift_compound = matches!(
            operator,
            k if k == SyntaxKind::LessThanLessThanEqualsToken as u16
                || k == SyntaxKind::GreaterThanGreaterThanEqualsToken as u16
                || k == SyntaxKind::GreaterThanGreaterThanGreaterThanEqualsToken as u16
        );
        if is_boolean_bitwise_compound && !is_function_assignment {
            // TS2447: For &=, |=, ^= with both boolean operands, emit special error
            let evaluator = tsz_solver::BinaryOpEvaluator::new(self.ctx.types);
            let left_is_boolean = evaluator.is_boolean_like(left_type);
            let right_is_boolean = evaluator.is_boolean_like(right_type);
            if left_is_boolean && right_is_boolean {
                let (op_str, suggestion) = match operator {
                    k if k == SyntaxKind::AmpersandEqualsToken as u16 => ("&=", "&&"),
                    k if k == SyntaxKind::BarEqualsToken as u16 => ("|=", "||"),
                    _ => ("^=", "!=="),
                };
                self.emit_boolean_operator_error(left_idx, op_str, suggestion);
                emitted_operator_error = true;
            } else if left_type != TypeId::ERROR && right_type != TypeId::ERROR {
                emitted_operator_error |=
                    self.check_arithmetic_operands(left_idx, right_idx, left_type, right_type);
            }
        } else if is_shift_compound
            && !is_function_assignment
            && left_type != TypeId::ERROR
            && right_type != TypeId::ERROR
        {
            emitted_operator_error |=
                self.check_arithmetic_operands(left_idx, right_idx, left_type, right_type);
        }

        let result_type = self.compound_assignment_result_type(left_type, right_type, operator);
        let is_logical_assignment = matches!(
            operator,
            k if k == SyntaxKind::AmpersandAmpersandEqualsToken as u16
                || k == SyntaxKind::BarBarEqualsToken as u16
                || k == SyntaxKind::QuestionQuestionEqualsToken as u16
        );
        let assigned_type = if is_logical_assignment {
            right_type
        } else {
            result_type
        };

        if left_type != TypeId::ANY && !emitted_operator_error {
            self.check_assignment_compatibility(
                left_idx,
                right_idx,
                assigned_type,
                left_type,
                true,
                false,
            );

            if left_type != TypeId::UNKNOWN
                && let Some(right_node) = self.ctx.arena.get(right_idx)
                && right_node.kind == tsz_parser::parser::syntax_kind_ext::OBJECT_LITERAL_EXPRESSION
            {
                self.check_object_literal_excess_properties(right_type, left_type, right_idx);
            }
        }

        result_type
    }

    /// Compute the result type of a compound assignment operator.
    ///
    /// This function determines what type a compound assignment expression
    /// produces based on the operator and operand types.
    fn compound_assignment_result_type(
        &self,
        left_type: TypeId,
        right_type: TypeId,
        operator: u16,
    ) -> TypeId {
        use tsz_solver::{BinaryOpEvaluator, BinaryOpResult};

        let evaluator = BinaryOpEvaluator::new(self.ctx.types);
        let op_str = match operator {
            k if k == SyntaxKind::PlusEqualsToken as u16 => Some("+"),
            k if k == SyntaxKind::MinusEqualsToken as u16 => Some("-"),
            k if k == SyntaxKind::AsteriskEqualsToken as u16 => Some("*"),
            k if k == SyntaxKind::AsteriskAsteriskEqualsToken as u16 => Some("*"),
            k if k == SyntaxKind::SlashEqualsToken as u16 => Some("/"),
            k if k == SyntaxKind::PercentEqualsToken as u16 => Some("%"),
            k if k == SyntaxKind::AmpersandAmpersandEqualsToken as u16 => Some("&&"),
            k if k == SyntaxKind::BarBarEqualsToken as u16 => Some("||"),
            _ => None,
        };

        if let Some(op) = op_str {
            return match evaluator.evaluate(left_type, right_type, op) {
                BinaryOpResult::Success(result) => result,
                // Return ANY instead of UNKNOWN for type errors to prevent cascading errors
                BinaryOpResult::TypeError { .. } => TypeId::ANY,
            };
        }

        if operator == SyntaxKind::QuestionQuestionEqualsToken as u16 {
            let factory = self.ctx.types.factory();
            return factory.union(vec![left_type, right_type]);
        }

        if matches!(
            operator,
            k if k == SyntaxKind::AmpersandEqualsToken as u16
                || k == SyntaxKind::BarEqualsToken as u16
                || k == SyntaxKind::CaretEqualsToken as u16
                || k == SyntaxKind::LessThanLessThanEqualsToken as u16
                || k == SyntaxKind::GreaterThanGreaterThanEqualsToken as u16
                || k == SyntaxKind::GreaterThanGreaterThanGreaterThanEqualsToken as u16
        ) {
            return TypeId::NUMBER;
        }

        // Return ANY for unknown binary operand types to prevent cascading errors
        TypeId::ANY
    }

    fn check_assignment_compatibility(
        &mut self,
        left_idx: NodeIndex,
        right_idx: NodeIndex,
        source_type: TypeId,
        target_type: TypeId,
        check_assignability: bool,
        suppress_error_for_error_types: bool,
    ) {
        if let Some((source_level, target_level)) =
            self.constructor_accessibility_mismatch_for_assignment(left_idx, right_idx)
        {
            self.error_constructor_accessibility_not_assignable(
                source_type,
                target_type,
                source_level,
                target_level,
                right_idx,
            );
            return;
        }

        if !check_assignability {
            return;
        }

        if suppress_error_for_error_types
            && (source_type == TypeId::ERROR || target_type == TypeId::ERROR)
        {
            return;
        }

        // TS2322 anchoring should point at the assignment target (LHS), not the RHS expression.
        // This aligns diagnostic fingerprints with tsc for assignment-compatibility suites.
        let _ = self.check_assignable_or_report_at(source_type, target_type, right_idx, left_idx);
    }
}