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
//! Type assignability and excess property checking.
//!
//! Subtype, identity, and redeclaration compatibility live in
//! `subtype_identity_checker`.
use crate::query_boundaries::assignability::{
AssignabilityEvalKind, AssignabilityQueryInputs, ExcessPropertiesKind,
are_types_overlapping_with_env, check_assignable_gate_with_overrides,
classify_for_assignability_eval, classify_for_excess_properties,
is_assignable_bivariant_with_resolver, is_assignable_with_overrides, is_callable_type,
is_relation_cacheable, object_shape_for_type,
};
use crate::state::{CheckerOverrideProvider, CheckerState};
use rustc_hash::FxHashSet;
use tracing::trace;
use tsz_common::interner::Atom;
use tsz_parser::parser::NodeIndex;
use tsz_parser::parser::node::NodeAccess;
use tsz_parser::parser::node_flags;
use tsz_parser::parser::syntax_kind_ext;
use tsz_scanner::SyntaxKind;
use tsz_solver::NarrowingContext;
use tsz_solver::RelationCacheKey;
use tsz_solver::TypeId;
use tsz_solver::visitor::{collect_lazy_def_ids, collect_type_queries};
// =============================================================================
// Assignability Checking Methods
// =============================================================================
impl<'a> CheckerState<'a> {
fn get_keyof_type_keys(
&mut self,
type_id: TypeId,
db: &dyn tsz_solver::TypeDatabase,
) -> FxHashSet<Atom> {
if let Some(keyof_type) = tsz_solver::type_queries::get_keyof_type(db, type_id)
&& let Some(key_type) =
tsz_solver::type_queries::keyof_object_properties(db, keyof_type)
&& let Some(members) = tsz_solver::type_queries::get_union_members(db, key_type)
{
return members
.into_iter()
.filter_map(|m| {
if let Some(str_lit) = tsz_solver::type_queries::get_string_literal_value(db, m)
{
return Some(str_lit);
}
None
})
.collect();
}
FxHashSet::default()
}
fn typeof_this_comparison_literal(
&self,
left: NodeIndex,
right: NodeIndex,
this_ref: NodeIndex,
) -> Option<&str> {
if self.is_typeof_this_target(left, this_ref) {
return self.string_literal_text(right);
}
if self.is_typeof_this_target(right, this_ref) {
return self.string_literal_text(left);
}
None
}
fn is_typeof_this_target(&self, expr: NodeIndex, this_ref: NodeIndex) -> bool {
let expr = self.ctx.arena.skip_parenthesized(expr);
let Some(node) = self.ctx.arena.get(expr) else {
return false;
};
if node.kind != syntax_kind_ext::PREFIX_UNARY_EXPRESSION {
return false;
}
let Some(unary) = self.ctx.arena.get_unary_expr(node) else {
return false;
};
if unary.operator != SyntaxKind::TypeOfKeyword as u16 {
return false;
}
let operand = self.ctx.arena.skip_parenthesized(unary.operand);
if operand == this_ref {
return true;
}
self.ctx
.arena
.get(operand)
.is_some_and(|n| n.kind == SyntaxKind::ThisKeyword as u16)
}
fn string_literal_text(&self, idx: NodeIndex) -> Option<&str> {
let idx = self.ctx.arena.skip_parenthesized(idx);
let node = self.ctx.arena.get(idx)?;
if node.kind == SyntaxKind::StringLiteral as u16
|| node.kind == SyntaxKind::NoSubstitutionTemplateLiteral as u16
{
return self
.ctx
.arena
.get_literal(node)
.map(|lit| lit.text.as_str());
}
None
}
fn narrow_this_from_enclosing_typeof_guard(
&self,
source_idx: NodeIndex,
source: TypeId,
) -> TypeId {
let is_this_source = self
.ctx
.arena
.get(source_idx)
.is_some_and(|n| n.kind == SyntaxKind::ThisKeyword as u16);
if !is_this_source {
return source;
}
let mut current = source_idx;
let mut depth = 0usize;
while depth < 256 {
depth += 1;
let Some(ext) = self.ctx.arena.get_extended(current) else {
break;
};
if ext.parent.is_none() {
break;
}
current = ext.parent;
let Some(parent_node) = self.ctx.arena.get(current) else {
break;
};
if parent_node.kind != syntax_kind_ext::IF_STATEMENT {
continue;
}
let Some(if_stmt) = self.ctx.arena.get_if_statement(parent_node) else {
continue;
};
if !self.is_node_within(source_idx, if_stmt.then_statement) {
continue;
}
let Some(cond_node) = self.ctx.arena.get(if_stmt.expression) else {
continue;
};
if cond_node.kind != syntax_kind_ext::BINARY_EXPRESSION {
continue;
}
let Some(bin) = self.ctx.arena.get_binary_expr(cond_node) else {
continue;
};
let is_equality = bin.operator_token == SyntaxKind::EqualsEqualsEqualsToken as u16
|| bin.operator_token == SyntaxKind::EqualsEqualsToken as u16;
if !is_equality {
continue;
}
if let Some(type_name) =
self.typeof_this_comparison_literal(bin.left, bin.right, source_idx)
{
return NarrowingContext::new(self.ctx.types).narrow_by_typeof(source, type_name);
}
}
source
}
/// Ensure relation preconditions (lazy refs + application symbols) for one type.
pub(crate) fn ensure_relation_input_ready(&mut self, type_id: TypeId) {
self.ensure_refs_resolved(type_id);
self.ensure_application_symbols_resolved(type_id);
}
/// Ensure relation preconditions (lazy refs + application symbols) for multiple types.
pub(crate) fn ensure_relation_inputs_ready(&mut self, type_ids: &[TypeId]) {
for &type_id in type_ids {
self.ensure_relation_input_ready(type_id);
}
}
/// Centralized suppression for TS2322-style assignability diagnostics.
pub(crate) const fn should_suppress_assignability_diagnostic(
&self,
source: TypeId,
target: TypeId,
) -> bool {
matches!(source, TypeId::ERROR | TypeId::ANY)
|| matches!(target, TypeId::ERROR | TypeId::ANY)
}
/// Suppress assignability diagnostics when they are likely parser-recovery artifacts.
///
/// In files with real syntax errors, we often get placeholder nodes and transient
/// parse states. Checker-level semantics should not emit TS2322 there.
fn should_suppress_assignability_for_parse_recovery(
&self,
source_idx: NodeIndex,
diag_idx: NodeIndex,
) -> bool {
if !self.has_syntax_parse_errors() {
return false;
}
if self.ctx.syntax_parse_error_positions.is_empty() {
return false;
}
self.is_parse_recovery_anchor_node(source_idx)
|| self.is_parse_recovery_anchor_node(diag_idx)
}
/// Detect nodes that look like parser-recovery artifacts.
///
/// Recovery heuristics:
/// - Missing-expression placeholders are currently identifiers with empty text.
/// - Nodes that start very near a syntax parse error are considered unstable.
/// - Nodes in subtrees that were marked as parse-recovery by the parser are suppressed.
fn is_parse_recovery_anchor_node(&self, idx: NodeIndex) -> bool {
let Some(node) = self.ctx.arena.get(idx) else {
return false;
};
// Missing-expression placeholders used by parser recovery.
if self
.ctx
.arena
.get_identifier_text(idx)
.is_some_and(str::is_empty)
{
return true;
}
// Also suppress diagnostics anchored very near a syntax parse error.
const DIAG_PARSE_DISTANCE: u32 = 16;
for &err_pos in &self.ctx.syntax_parse_error_positions {
let before = err_pos.saturating_sub(DIAG_PARSE_DISTANCE);
let after = err_pos.saturating_add(DIAG_PARSE_DISTANCE);
if (node.pos >= before && node.pos <= after)
|| (node.end >= before && node.end <= after)
{
return true;
}
}
let mut current = idx;
let mut walk_guard = 0;
while current.is_some() {
walk_guard += 1;
if walk_guard > 512 {
break;
}
if let Some(current_node) = self.ctx.arena.get(current) {
let flags = current_node.flags as u32;
if (flags & node_flags::THIS_NODE_HAS_ERROR) != 0
|| (flags & node_flags::THIS_NODE_OR_ANY_SUB_NODES_HAS_ERROR) != 0
{
return true;
}
} else {
break;
}
let Some(ext) = self.ctx.arena.get_extended(current) else {
break;
};
if ext.parent.is_none() {
break;
}
current = ext.parent;
}
false
}
// =========================================================================
// Type Evaluation for Assignability
// =========================================================================
/// Ensure all Ref types in a type are resolved and in the type environment.
///
/// This is critical for intersection/union type assignability. When we have
/// `type AB = A & B`, the intersection contains Ref(A) and Ref(B). Before we
/// can check assignability against the intersection, we need to ensure A and B
/// are resolved and in `type_env` so the subtype checker can resolve them.
pub(crate) fn ensure_refs_resolved(&mut self, type_id: TypeId) {
let mut visited_types = FxHashSet::default();
let mut visited_def_ids = FxHashSet::default();
let mut worklist = vec![type_id];
while let Some(current) = worklist.pop() {
if !visited_types.insert(current) {
continue;
}
for symbol_ref in collect_type_queries(self.ctx.types, current) {
let sym_id = tsz_binder::SymbolId(symbol_ref.0);
let _ = self.get_type_of_symbol(sym_id);
}
for def_id in collect_lazy_def_ids(self.ctx.types, current) {
if !visited_def_ids.insert(def_id) {
continue;
}
if let Some(result) = self.resolve_and_insert_def_type(def_id)
&& result != TypeId::ERROR
&& result != TypeId::ANY
{
worklist.push(result);
}
}
}
}
/// Evaluate a type for assignability checking.
///
/// Determines if the type needs evaluation (applications, env-dependent types)
/// and performs the appropriate evaluation.
pub(crate) fn evaluate_type_for_assignability(&mut self, type_id: TypeId) -> TypeId {
let mut evaluated = match classify_for_assignability_eval(self.ctx.types, type_id) {
AssignabilityEvalKind::Application => self.evaluate_type_with_resolution(type_id),
AssignabilityEvalKind::NeedsEnvEval => self.evaluate_type_with_env(type_id),
AssignabilityEvalKind::Resolved => type_id,
};
// Distribution pass: normalize compound types so mixed representations do not
// leak into relation checks (for example, `Lazy(Class)` + resolved class object).
if let Some(distributed) =
tsz_solver::type_queries::map_compound_members(self.ctx.types, evaluated, |member| {
self.evaluate_type_for_assignability(member)
})
{
evaluated = distributed;
}
evaluated
}
// =========================================================================
// Main Assignability Check
// =========================================================================
/// Substitute `ThisType` in a type with the enclosing class instance type.
///
/// When inside a class body, `ThisType` represents the polymorphic `this` type
/// (a type parameter bounded by the class). Since the `this` expression evaluates
/// to the concrete class instance type, we must substitute `ThisType` → class
/// instance type before assignability checks. This matches tsc's behavior where
/// `return this`, `f(this)`, etc. succeed when the target type is `this`.
fn substitute_this_type_if_needed(&mut self, type_id: TypeId) -> TypeId {
// Fast path: intrinsic types can't contain ThisType
if type_id.is_intrinsic() {
return type_id;
}
let needs_substitution = tsz_solver::is_this_type(self.ctx.types, type_id);
if !needs_substitution {
return type_id;
}
let Some(class_info) = &self.ctx.enclosing_class else {
return type_id;
};
let class_idx = class_info.class_idx;
let Some(node) = self.ctx.arena.get(class_idx) else {
return type_id;
};
let Some(class_data) = self.ctx.arena.get_class(node) else {
return type_id;
};
let instance_type = self.get_class_instance_type(class_idx, class_data);
if tsz_solver::is_this_type(self.ctx.types, type_id) {
instance_type
} else {
tsz_solver::substitute_this_type(self.ctx.types, type_id, instance_type)
}
}
/// Check if source type is assignable to target type.
///
/// This is the main entry point for assignability checking, used throughout
/// the type system to validate assignments, function calls, returns, etc.
/// Assignability is more permissive than subtyping.
pub fn is_assignable_to(&mut self, source: TypeId, target: TypeId) -> bool {
// CRITICAL: Ensure all Ref types are resolved before assignability check.
// This fixes intersection type assignability where `type AB = A & B` needs
// A and B in type_env before we can check if a type is assignable to the intersection.
self.ensure_relation_input_ready(target);
// Substitute `ThisType` in the target with the class instance type.
// In tsc, `this` acts as a type parameter constrained to the class type.
// The `this` expression evaluates to the concrete class instance type, so when
// the target (return type, parameter type, etc.) contains `ThisType`, we need to
// resolve it to the class instance type before the assignability check.
let target = self.substitute_this_type_if_needed(target);
// Pre-check: Function interface accepts any callable type.
// Must check before evaluate_type_for_assignability resolves Lazy(DefId)
// to ObjectShape, losing the DefId identity needed to recognize it as Function.
{
use tsz_solver::visitor::lazy_def_id;
let is_function_target = lazy_def_id(self.ctx.types, target).is_some_and(|t_def| {
self.ctx.type_env.try_borrow().ok().is_some_and(|env| {
env.is_boxed_def_id(t_def, tsz_solver::IntrinsicKind::Function)
})
});
if is_function_target {
let source_eval = self.evaluate_type_for_assignability(source);
if is_callable_type(self.ctx.types, source_eval) {
return true;
}
}
}
let source = self.evaluate_type_for_assignability(source);
let target = self.evaluate_type_for_assignability(target);
// Check relation cache for non-inference types
// Construct RelationCacheKey with Lawyer-layer flags to prevent cache poisoning
// Note: Use ORIGINAL types for cache key, not evaluated types
let is_cacheable = is_relation_cacheable(self.ctx.types, source, target);
let flags = self.ctx.pack_relation_flags();
if is_cacheable {
let cache_key = RelationCacheKey::assignability(source, target, flags, 0);
if let Some(cached) = self.ctx.types.lookup_assignability_cache(cache_key) {
return cached;
}
}
// Use CheckerContext as the resolver instead of TypeEnvironment
// This enables access to symbol information for enum type detection
let overrides = CheckerOverrideProvider::new(self, None);
let result = is_assignable_with_overrides(
&AssignabilityQueryInputs {
db: self.ctx.types,
resolver: &self.ctx,
source,
target,
flags,
inheritance_graph: &self.ctx.inheritance_graph,
sound_mode: self.ctx.sound_mode(),
},
&overrides,
);
if is_cacheable {
let cache_key = RelationCacheKey::assignability(source, target, flags, 0);
self.ctx.types.insert_assignability_cache(cache_key, result);
}
trace!(
source = source.0,
target = target.0,
result,
"is_assignable_to"
);
// Add keyof type checking logic
if let Some(keyof_type) = tsz_solver::type_queries::get_keyof_type(self.ctx.types, target)
&& let Some(source_atom) =
tsz_solver::type_queries::get_string_literal_value(self.ctx.types, source)
{
let source_str = self.ctx.types.resolve_atom(source_atom);
let allowed_keys =
tsz_solver::type_queries::get_allowed_keys(self.ctx.types, keyof_type);
if !allowed_keys.contains(&source_str) {
return false;
}
}
result
}
///
/// This keeps the same checker gateway (resolver + overrides + caches) as
/// `is_assignable_to`, but forces the strict-function-types relation flag.
pub fn is_assignable_to_strict(&mut self, source: TypeId, target: TypeId) -> bool {
self.ensure_relation_input_ready(target);
let target = self.substitute_this_type_if_needed(target);
let source = self.evaluate_type_for_assignability(source);
let target = self.evaluate_type_for_assignability(target);
let is_cacheable = is_relation_cacheable(self.ctx.types, source, target);
let flags = self.ctx.pack_relation_flags() | RelationCacheKey::FLAG_STRICT_FUNCTION_TYPES;
if is_cacheable {
let cache_key = RelationCacheKey::assignability(source, target, flags, 0);
if let Some(cached) = self.ctx.types.lookup_assignability_cache(cache_key) {
return cached;
}
}
let overrides = CheckerOverrideProvider::new(self, None);
let result = is_assignable_with_overrides(
&AssignabilityQueryInputs {
db: self.ctx.types,
resolver: &self.ctx,
source,
target,
flags,
inheritance_graph: &self.ctx.inheritance_graph,
sound_mode: self.ctx.sound_mode(),
},
&overrides,
);
if is_cacheable {
let cache_key = RelationCacheKey::assignability(source, target, flags, 0);
self.ctx.types.insert_assignability_cache(cache_key, result);
}
trace!(
source = source.0,
target = target.0,
result,
"is_assignable_to_strict"
);
result
}
/// Check assignability while forcing strict null checks in relation flags.
///
/// This keeps the regular checker/solver assignability gateway (resolver,
/// overrides, caching, and precondition setup) while pinning nullability
/// semantics to strict mode for localized checks.
pub fn is_assignable_to_strict_null(&mut self, source: TypeId, target: TypeId) -> bool {
self.ensure_relation_input_ready(target);
let target = self.substitute_this_type_if_needed(target);
let source = self.evaluate_type_for_assignability(source);
let target = self.evaluate_type_for_assignability(target);
let is_cacheable = is_relation_cacheable(self.ctx.types, source, target);
let flags = self.ctx.pack_relation_flags() | RelationCacheKey::FLAG_STRICT_NULL_CHECKS;
if is_cacheable {
let cache_key = RelationCacheKey::assignability(source, target, flags, 0);
if let Some(cached) = self.ctx.types.lookup_assignability_cache(cache_key) {
return cached;
}
}
let overrides = CheckerOverrideProvider::new(self, None);
let result = is_assignable_with_overrides(
&AssignabilityQueryInputs {
db: self.ctx.types,
resolver: &self.ctx,
source,
target,
flags,
inheritance_graph: &self.ctx.inheritance_graph,
sound_mode: self.ctx.sound_mode(),
},
&overrides,
);
if is_cacheable {
let cache_key = RelationCacheKey::assignability(source, target, flags, 0);
self.ctx.types.insert_assignability_cache(cache_key, result);
}
trace!(
source = source.0,
target = target.0,
result,
"is_assignable_to_strict_null"
);
result
}
/// Check if `source` type is assignable to `target` type, resolving Ref types.
///
/// Uses the provided `TypeEnvironment` to resolve type references.
pub fn is_assignable_to_with_env(
&self,
source: TypeId,
target: TypeId,
env: &tsz_solver::TypeEnvironment,
) -> bool {
let flags = self.ctx.pack_relation_flags();
let overrides = CheckerOverrideProvider::new(self, Some(env));
is_assignable_with_overrides(
&AssignabilityQueryInputs {
db: self.ctx.types,
resolver: env,
source,
target,
flags,
inheritance_graph: &self.ctx.inheritance_graph,
sound_mode: self.ctx.sound_mode(),
},
&overrides,
)
}
/// Check if `source` type is assignable to `target` type with bivariant function parameter checking.
///
/// This is used for class method override checking, where methods are always bivariant
/// (unlike function properties which are contravariant with strictFunctionTypes).
///
/// Follows the same pattern as `is_assignable_to` but calls `is_assignable_to_bivariant_callback`
/// which disables `strict_function_types` for the check.
pub fn is_assignable_to_bivariant(&mut self, source: TypeId, target: TypeId) -> bool {
// CRITICAL: Ensure all Ref types are resolved before assignability check.
// This fixes intersection type assignability where `type AB = A & B` needs
// A and B in type_env before we can check if a type is assignable to the intersection.
self.ensure_relation_input_ready(target);
let source = self.evaluate_type_for_assignability(source);
let target = self.evaluate_type_for_assignability(target);
// Check relation cache for non-inference types
// Construct RelationCacheKey with Lawyer-layer flags to prevent cache poisoning
// Note: Use ORIGINAL types for cache key, not evaluated types
let is_cacheable = is_relation_cacheable(self.ctx.types, source, target);
// For bivariant checks, we strip the strict_function_types flag
// so the cache key is distinct from regular assignability checks.
let flags = self.ctx.pack_relation_flags() & !RelationCacheKey::FLAG_STRICT_FUNCTION_TYPES;
if is_cacheable {
// Note: For assignability checks, we use AnyPropagationMode::All (0)
// since the checker doesn't track depth like SubtypeChecker does
let cache_key = RelationCacheKey::assignability(source, target, flags, 0);
if let Some(cached) = self.ctx.types.lookup_assignability_cache(cache_key) {
return cached;
}
}
let env = self.ctx.type_env.borrow();
// Preserve existing behavior: bivariant path does not use checker overrides.
let result = is_assignable_bivariant_with_resolver(
self.ctx.types,
&*env,
source,
target,
flags,
&self.ctx.inheritance_graph,
self.ctx.sound_mode(),
);
// Cache the result for non-inference types
// Use ORIGINAL types for cache key (not evaluated types)
if is_cacheable {
let cache_key = RelationCacheKey::assignability(source, target, flags, 0);
self.ctx.types.insert_assignability_cache(cache_key, result);
}
trace!(
source = source.0,
target = target.0,
result,
"is_assignable_to_bivariant"
);
result
}
/// Check if two types have any overlap (can ever be equal).
///
/// Used for TS2367: "This condition will always return 'false'/'true' since
/// the types 'X' and 'Y' have no overlap."
///
/// Returns true if the types can potentially be equal, false if they can never
/// have any common value.
pub fn are_types_overlapping(&mut self, left: TypeId, right: TypeId) -> bool {
// Ensure centralized relation preconditions before overlap check.
self.ensure_relation_input_ready(left);
self.ensure_relation_input_ready(right);
let env = self.ctx.type_env.borrow();
are_types_overlapping_with_env(
self.ctx.types,
&env,
left,
right,
self.ctx.strict_null_checks(),
)
}
// =========================================================================
// Weak Union and Excess Property Checking
// =========================================================================
/// Check if we should skip the general assignability error for an object literal.
/// Returns true if:
/// 1. It's a weak union violation (TypeScript shows excess property error instead)
/// 2. OR if the object literal has excess properties (TypeScript prioritizes TS2353 over TS2345/TS2322)
pub(crate) fn should_skip_weak_union_error(
&mut self,
source: TypeId,
target: TypeId,
source_idx: NodeIndex,
) -> bool {
let Some(node) = self.ctx.arena.get(source_idx) else {
return false;
};
if node.kind != syntax_kind_ext::OBJECT_LITERAL_EXPRESSION {
return false;
}
// Check for weak union violation first (using scoped borrow)
if self.is_weak_union_violation(source, target) {
return true;
}
// Check if there are excess properties.
if !self.object_literal_has_excess_properties(source, target, source_idx) {
return false;
}
// There are excess properties. Check if all matching properties have compatible types.
let Some(source_shape) = object_shape_for_type(self.ctx.types, source) else {
return true;
};
let resolved_target = self.resolve_type_for_property_access(target);
let Some(target_shape) = object_shape_for_type(self.ctx.types, resolved_target) else {
return true;
};
let source_props = source_shape.properties.as_slice();
let target_props = target_shape.properties.as_slice();
// Check if any source property that exists in target has a wrong type
for source_prop in source_props {
if let Some(target_prop) = target_props.iter().find(|p| p.name == source_prop.name) {
let source_prop_type = source_prop.type_id;
let target_prop_type = target_prop.type_id;
let effective_target_type = if target_prop.optional {
self.ctx
.types
.union(vec![target_prop_type, TypeId::UNDEFINED])
} else {
target_prop_type
};
let is_assignable =
{ self.is_assignable_to(source_prop_type, effective_target_type) };
if !is_assignable {
return false;
}
}
}
true
}
/// Check assignability and emit the standard TS2322/TS2345-style diagnostic when needed.
pub(crate) fn check_satisfies_assignable_or_report(
&mut self,
source: TypeId,
target: TypeId,
source_idx: NodeIndex,
) -> bool {
let diag_idx = source_idx;
let source = self.narrow_this_from_enclosing_typeof_guard(source_idx, source);
if self.should_suppress_assignability_diagnostic(source, target) {
return true;
}
if self.should_suppress_assignability_for_parse_recovery(source_idx, diag_idx) {
return true;
}
if tsz_solver::type_queries::is_keyof_type(self.ctx.types, target)
&& let Some(str_lit) =
tsz_solver::type_queries::get_string_literal_value(self.ctx.types, source)
{
let keyof_type =
tsz_solver::type_queries::get_keyof_type(self.ctx.types, target).unwrap();
let allowed_keys = self.get_keyof_type_keys(keyof_type, self.ctx.types);
if !allowed_keys.contains(&str_lit) {
self.error_type_does_not_satisfy_the_expected_type(source, target, diag_idx);
return false;
}
}
if let Some(node) = self.ctx.arena.get(source_idx)
&& node.kind == syntax_kind_ext::OBJECT_LITERAL_EXPRESSION
{
self.check_object_literal_excess_properties(source, target, source_idx);
}
if self.is_assignable_to(source, target)
|| self.should_skip_weak_union_error(source, target, source_idx)
{
return true;
}
self.error_type_does_not_satisfy_the_expected_type(source, target, diag_idx);
false
}
///
/// Returns true when no diagnostic was emitted (assignable or intentionally skipped),
/// false when an assignability diagnostic was emitted.
pub(crate) fn check_assignable_or_report(
&mut self,
source: TypeId,
target: TypeId,
source_idx: NodeIndex,
) -> bool {
self.check_assignable_or_report_at(source, target, source_idx, source_idx)
}
/// Check assignability and emit TS2322/TS2345-style diagnostics with independent
/// source and diagnostic anchors.
///
/// `source_idx` is used for weak-union/excess-property prioritization.
/// `diag_idx` is where the assignability diagnostic is anchored.
pub(crate) fn check_assignable_or_report_at(
&mut self,
source: TypeId,
target: TypeId,
source_idx: NodeIndex,
diag_idx: NodeIndex,
) -> bool {
let source = self.narrow_this_from_enclosing_typeof_guard(source_idx, source);
if self.should_suppress_assignability_diagnostic(source, target) {
return true;
}
if self.should_suppress_assignability_for_parse_recovery(source_idx, diag_idx) {
return true;
}
if tsz_solver::type_queries::is_keyof_type(self.ctx.types, target)
&& let Some(str_lit) =
tsz_solver::type_queries::get_string_literal_value(self.ctx.types, source)
{
let keyof_type =
tsz_solver::type_queries::get_keyof_type(self.ctx.types, target).unwrap();
let allowed_keys = self.get_keyof_type_keys(keyof_type, self.ctx.types);
if !allowed_keys.contains(&str_lit) {
self.error_type_not_assignable_with_reason_at(source, target, diag_idx);
return false;
}
}
if self.is_assignable_to(source, target)
|| self.should_skip_weak_union_error(source, target, source_idx)
{
return true;
}
self.error_type_not_assignable_with_reason_at(source, target, diag_idx);
false
}
/// Check assignability and emit a generic TS2322 diagnostic at `diag_idx`.
///
/// This is used for call sites that intentionally avoid detailed reason rendering
/// but still share centralized mismatch/suppression behavior.
pub(crate) fn check_assignable_or_report_generic_at(
&mut self,
source: TypeId,
target: TypeId,
source_idx: NodeIndex,
diag_idx: NodeIndex,
) -> bool {
let source = self.narrow_this_from_enclosing_typeof_guard(source_idx, source);
if self.should_suppress_assignability_diagnostic(source, target) {
return true;
}
if self.should_suppress_assignability_for_parse_recovery(source_idx, diag_idx) {
return true;
}
if self.is_assignable_to(source, target)
|| self.should_skip_weak_union_error(source, target, source_idx)
{
return true;
}
self.error_type_not_assignable_generic_at(source, target, diag_idx);
false
}
/// Check assignability and emit argument-not-assignable diagnostics (TS2345-style).
///
/// Returns true when no diagnostic was emitted (assignable or intentionally skipped),
/// false when an argument-assignability diagnostic was emitted.
pub(crate) fn check_argument_assignable_or_report(
&mut self,
source: TypeId,
target: TypeId,
arg_idx: NodeIndex,
) -> bool {
if self.should_suppress_assignability_diagnostic(source, target) {
return true;
}
if self.should_suppress_assignability_for_parse_recovery(arg_idx, arg_idx) {
return true;
}
if self.is_assignable_to(source, target) {
return true;
}
if self.should_skip_weak_union_error(source, target, arg_idx) {
return true;
}
self.error_argument_not_assignable_at(source, target, arg_idx);
false
}
/// Returns true when an assignability mismatch should produce a diagnostic.
///
/// This centralizes the standard "not assignable + not weak-union/excess-property
/// suppression" decision so call sites emitting different diagnostics can share it.
pub(crate) fn should_report_assignability_mismatch(
&mut self,
source: TypeId,
target: TypeId,
source_idx: NodeIndex,
) -> bool {
if self.should_suppress_assignability_diagnostic(source, target) {
return false;
}
if self.should_suppress_assignability_for_parse_recovery(source_idx, source_idx) {
return false;
}
!self.is_assignable_to(source, target)
&& !self.should_skip_weak_union_error(source, target, source_idx)
}
/// Returns true when a bivariant-assignability mismatch should produce a diagnostic.
///
/// Mirrors `should_report_assignability_mismatch` but uses the bivariant relation
/// entrypoint for method-compatibility scenarios.
pub(crate) fn should_report_assignability_mismatch_bivariant(
&mut self,
source: TypeId,
target: TypeId,
source_idx: NodeIndex,
) -> bool {
if self.should_suppress_assignability_diagnostic(source, target) {
return false;
}
if self.should_suppress_assignability_for_parse_recovery(source_idx, source_idx) {
return false;
}
!self.is_assignable_to_bivariant(source, target)
&& !self.should_skip_weak_union_error(source, target, source_idx)
}
/// Check bidirectional assignability.
///
/// Useful in checker locations that need type comparability/equivalence-like checks.
pub(crate) fn are_mutually_assignable(&mut self, left: TypeId, right: TypeId) -> bool {
self.is_assignable_to(left, right) && self.is_assignable_to(right, left)
}
/// Check if two types are comparable (overlap).
///
/// Corresponds to TypeScript's `isTypeComparableTo`: returns true if the types
/// have any overlap. TSC's comparableRelation differs from assignability:
/// - For union sources: uses `someTypeRelatedToType` (ANY member suffices)
/// - For union targets: also checks per-member overlap
///
/// Used for switch/case comparability (TS2678), equality narrowing, etc.
pub(crate) fn is_type_comparable_to(&mut self, source: TypeId, target: TypeId) -> bool {
// Fast path: direct bidirectional assignability
if self.is_assignable_to(source, target) || self.is_assignable_to(target, source) {
return true;
}
// TSC's comparable relation decomposes unions and checks if ANY member
// is related to the other type. This handles cases like:
// - `User.A | User.B` comparable to `User.A` (User.A member matches)
// - `string & Brand` comparable to `"a"` (string member of intersection)
use crate::query_boundaries::dispatch as query;
// Decompose source union: check if any member is assignable in either direction
if let Some(members) = query::union_members(self.ctx.types, source) {
for member in &members {
if self.is_assignable_to(*member, target) || self.is_assignable_to(target, *member)
{
return true;
}
}
}
// Decompose target union: check if any member is assignable in either direction
if let Some(members) = query::union_members(self.ctx.types, target) {
for member in &members {
if self.is_assignable_to(source, *member) || self.is_assignable_to(*member, source)
{
return true;
}
}
}
false
}
/// Check if source object literal has properties that don't exist in target.
///
/// Uses TypeId-based freshness tracking (fresh object literals only).
pub(crate) fn object_literal_has_excess_properties(
&mut self,
source: TypeId,
target: TypeId,
_source_idx: NodeIndex,
) -> bool {
use tsz_solver::relations::freshness;
// Only fresh object literals trigger excess property checking.
if !freshness::is_fresh_object_type(self.ctx.types, source) {
return false;
}
let Some(source_shape) = object_shape_for_type(self.ctx.types, source) else {
return false;
};
let source_props = source_shape.properties.as_slice();
if source_props.is_empty() {
return false;
}
let resolved_target = self.resolve_type_for_property_access(target);
match classify_for_excess_properties(self.ctx.types, resolved_target) {
ExcessPropertiesKind::Object(shape_id) => {
let target_shape = self.ctx.types.object_shape(shape_id);
let target_props = target_shape.properties.as_slice();
if target_props.is_empty() {
return false;
}
if target_shape.string_index.is_some() || target_shape.number_index.is_some() {
return false;
}
source_props
.iter()
.any(|source_prop| !target_props.iter().any(|p| p.name == source_prop.name))
}
ExcessPropertiesKind::ObjectWithIndex(_shape_id) => false,
ExcessPropertiesKind::Union(members) => {
let mut target_shapes = Vec::new();
let mut matched_shapes = Vec::new();
for member in members {
let resolved_member = self.resolve_type_for_property_access(member);
let Some(shape) = object_shape_for_type(self.ctx.types, resolved_member) else {
// If a union member has no object shape and is a type parameter
// or the `object` intrinsic, it accepts any properties, so EPC
// should not apply.
if tsz_solver::type_queries::is_type_parameter_like(
self.ctx.types,
resolved_member,
) || resolved_member == TypeId::OBJECT
{
return false;
}
continue;
};
if shape.properties.is_empty()
|| shape.string_index.is_some()
|| shape.number_index.is_some()
{
return false;
}
target_shapes.push(shape.clone());
if self.is_subtype_of(source, resolved_member) {
matched_shapes.push(shape);
}
}
if target_shapes.is_empty() {
return false;
}
let effective_shapes = if matched_shapes.is_empty() {
target_shapes
} else {
matched_shapes
};
source_props.iter().any(|source_prop| {
!effective_shapes.iter().any(|shape| {
shape
.properties
.iter()
.any(|prop| prop.name == source_prop.name)
})
})
}
ExcessPropertiesKind::Intersection(members) => {
let mut target_shapes = Vec::new();
for member in members {
let resolved_member = self.resolve_type_for_property_access(member);
let Some(shape) = object_shape_for_type(self.ctx.types, resolved_member) else {
continue;
};
if shape.string_index.is_some() || shape.number_index.is_some() {
return false;
}
target_shapes.push(shape);
}
if target_shapes.is_empty() {
return false;
}
source_props.iter().any(|source_prop| {
!target_shapes.iter().any(|shape| {
shape
.properties
.iter()
.any(|prop| prop.name == source_prop.name)
})
})
}
ExcessPropertiesKind::NotObject => false,
}
}
pub(crate) fn analyze_assignability_failure(
&mut self,
source: TypeId,
target: TypeId,
) -> crate::query_boundaries::assignability::AssignabilityFailureAnalysis {
// Keep failure analysis on the same relation boundary as `is_assignable_to`
// (CheckerContext resolver + checker overrides) so mismatch suppression and
// diagnostic rendering observe identical compatibility semantics.
let overrides = CheckerOverrideProvider::new(self, None);
let inputs = AssignabilityQueryInputs {
db: self.ctx.types,
resolver: &self.ctx,
source,
target,
flags: self.ctx.pack_relation_flags(),
inheritance_graph: &self.ctx.inheritance_graph,
sound_mode: self.ctx.sound_mode(),
};
let gate = check_assignable_gate_with_overrides(&inputs, &overrides, Some(&self.ctx), true);
if gate.related {
return crate::query_boundaries::assignability::AssignabilityFailureAnalysis {
weak_union_violation: false,
failure_reason: None,
};
}
gate.analysis.unwrap_or(
crate::query_boundaries::assignability::AssignabilityFailureAnalysis {
weak_union_violation: false,
failure_reason: None,
},
)
}
pub(crate) fn is_weak_union_violation(&mut self, source: TypeId, target: TypeId) -> bool {
self.analyze_assignability_failure(source, target)
.weak_union_violation
}
}