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
//! Tests for TS2693 (type-only as value) and TS2362/TS2363 (arithmetic operand errors)

use crate::state::CheckerState;
use tsz_binder::BinderState;
use tsz_parser::parser::ParserState;
use tsz_solver::TypeInterner;

#[test]
fn test_interface_used_as_value() {
    let source = r"
interface Foo {
    a: number;
}
const x = new Foo();
";
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    // Should emit TS2693 for using interface as value
    let ts2693_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 2693)
        .count();
    assert!(
        ts2693_count >= 1,
        "Expected at least 1 TS2693 error, got {ts2693_count}"
    );
}

#[test]
fn test_type_alias_used_as_value() {
    let source = r"
type Foo = {
    a: number;
};
const x = new Foo();
";
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    // Should emit TS2693 for using type alias as value
    let ts2693_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 2693)
        .count();
    assert!(
        ts2693_count >= 1,
        "Expected at least 1 TS2693 error, got {ts2693_count}"
    );
}

/// For `number[]` parse-recovery, tsc only emits TS1011 (missing element access argument),
/// NOT TS2693. The parse error is sufficient — no need for a semantic "type used as value" error.
#[test]
fn test_primitive_array_type_recovery_no_ts2693() {
    let source = r"
var results = number[];
";
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    let ts2693_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 2693)
        .count();
    assert!(
        ts2693_count == 0,
        "Should NOT emit TS2693 for `number[]` parse-recovery (TS1011 is sufficient), got {ts2693_count}"
    );
}

#[test]
fn test_string_subtraction_emits_ts2362() {
    let source = r#"
const str = "hello";
const result = str - 5;
"#;
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    // Should emit TS2362 for left-hand side of - operation
    let ts2362_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 2362)
        .count();
    assert!(
        ts2362_count >= 1,
        "Expected at least 1 TS2362 error, got {ts2362_count}"
    );
}

#[test]
fn test_boolean_multiplication_emits_ts2362() {
    let source = r"
const flag = true;
const result = flag * 10;
";
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    // Should emit TS2362 for left-hand side of * operation
    let ts2362_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 2362)
        .count();
    assert!(
        ts2362_count >= 1,
        "Expected at least 1 TS2362 error, got {ts2362_count}"
    );
}

#[test]
fn test_number_divided_by_string_emits_ts2363() {
    let source = r#"
const num = 10;
const str = "hello";
const result = num / str;
"#;
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    // Should emit TS2363 for right-hand side of / operation
    let ts2363_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 2363)
        .count();
    assert!(
        ts2363_count >= 1,
        "Expected at least 1 TS2363 error, got {ts2363_count}"
    );
}

#[test]
fn test_arithmetic_on_non_numeric_types() {
    let source = r"
const obj = { a: 1 };
const arr = [1, 2, 3];
const r1 = obj - 1;  // TS2362
const r2 = 10 * arr;  // TS2363
const r3 = obj % 2;  // TS2362
";
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    // Should emit multiple TS2362 and TS2363 errors
    let ts2362_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 2362)
        .count();
    let ts2363_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 2363)
        .count();

    assert!(
        ts2362_count >= 2,
        "Expected at least 2 TS2362 errors, got {ts2362_count}"
    );
    assert!(
        ts2363_count >= 1,
        "Expected at least 1 TS2363 error, got {ts2363_count}"
    );
}

#[test]
fn test_valid_arithmetic_no_errors() {
    let source = r"
const a = 10;
const b = 5;
const r1 = a + b;  // OK - number addition
const r2 = a - b;  // OK - number subtraction
const r3 = a * b;  // OK - number multiplication
const r4 = a / b;  // OK - number division
const r5 = a % b;  // OK - number modulo
";
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    // Should not emit TS2362 or TS2363 for valid arithmetic
    let error_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 2362 || d.code == 2363)
        .count();
    assert_eq!(
        error_count, 0,
        "Expected no TS2362/TS2363 errors, got {error_count}"
    );
}

#[test]
fn test_for_of_variable_type_annotation_emits_ts2322() {
    let source = r"
const numbers = [1, 2, 3];
for (const x: string of numbers) {
    // Should emit TS2322: number is not assignable to string
}
";
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    // Should emit TS2322 for for-of variable with incompatible type annotation
    let ts2322_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 2322)
        .count();
    assert!(
        ts2322_count >= 1,
        "Expected at least 1 TS2322 error, got {ts2322_count}"
    );
}

#[test]
fn test_for_of_variable_compatible_type_no_error() {
    let source = r"
const numbers = [1, 2, 3];
for (const x: number of numbers) {
    // OK - number is assignable to number
}
";
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    // Should not emit TS2322 for compatible types
    let ts2322_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 2322)
        .count();
    assert_eq!(
        ts2322_count, 0,
        "Expected no TS2322 errors, got {ts2322_count}"
    );
}

#[test]
fn test_type_import_used_as_value() {
    // Test that type-only imports emit TS1361 when used as values
    // Note: In single-file mode, the imported module doesn't exist, so the
    // binder may not fully resolve the symbol. This test verifies that when
    // the symbol IS resolved, TS1361 is emitted instead of TS2693.
    let source = r"
import type { Foo } from './foo';
const x = new Foo();  // TS1361: Foo cannot be used as a value because it was imported using 'import type'
";
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    // In single-file mode, either TS1361 (type-only import as value) or
    // TS2693 (type used as value) should be emitted, depending on symbol resolution
    let type_value_errors: Vec<_> = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 1361 || d.code == 2693)
        .collect();
    assert!(
        !type_value_errors.is_empty() || {
            // Acceptable if no error when module can't be resolved in single-file mode
            checker.ctx.diagnostics.iter().any(|d| d.code == 2307)
        },
        "Expected TS1361/TS2693 or TS2307 (module not found), got: {:?}",
        checker
            .ctx
            .diagnostics
            .iter()
            .map(|d| d.code)
            .collect::<Vec<_>>()
    );
}

#[test]
fn test_interface_property_access_emits_ts18050() {
    // Test accessing interface as if it were an object with properties
    let source = r"
interface MyInterface {
    prop: string;
}
const x = MyInterface.prop;  // TS2693: MyInterface only refers to a type
";
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    // Should emit TS2693 for using interface as value
    let ts2693_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 2693)
        .count();
    assert!(
        ts2693_count >= 1,
        "Expected at least 1 TS2693 error for interface property access, got {ts2693_count}"
    );
}

#[test]
fn test_exponentiation_with_string_emits_ts2362() {
    // Test ** operator with string operand
    let source = r#"
const base = "2";
const exp = 3;
const result = base ** exp;  // TS2362: base is string, not number
"#;
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    // Should emit TS2362 for string operand in **
    let ts2362_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 2362)
        .count();
    assert!(
        ts2362_count >= 1,
        "Expected at least 1 TS2362 error for exponentiation, got {ts2362_count}"
    );
}

#[test]
fn test_bitwise_operations_with_invalid_operands() {
    // Test bitwise operators (&, |, ^, <<, >>, >>>) with non-integer types
    let source = r#"
const str = "test";
const obj = { a: 1 };
const r1 = str & 5;      // TS2362
const r2 = 10 | obj;     // TS2363
const r3 = obj ^ 2;      // TS2362
const r4 = 5 << str;     // TS2363
const r5 = str >> 1;     // TS2362
const r6 = 10 >>> obj;   // TS2363
"#;
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    // Should emit multiple TS2362 and TS2363 errors
    let ts2362_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 2362)
        .count();
    let ts2363_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 2363)
        .count();

    assert!(
        ts2362_count >= 3,
        "Expected at least 3 TS2362 errors for bitwise operations, got {ts2362_count}"
    );
    assert!(
        ts2363_count >= 3,
        "Expected at least 3 TS2363 errors for bitwise operations, got {ts2363_count}"
    );
}

#[test]
fn test_string_plus_number_no_error() {
    // Test that string + number is valid (string concatenation)
    let source = r#"
const str = "hello";
const num = 42;
const result = str + num;  // OK: string concatenation
"#;
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    // Should not emit TS2362/TS2363 for string + number (valid concatenation)
    let error_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 2362 || d.code == 2363)
        .count();
    assert_eq!(
        error_count, 0,
        "Expected no TS2362/TS2363 errors for string + number, got {error_count}"
    );
}

#[test]
fn test_enum_arithmetic_valid() {
    // Test that enum members can be used in arithmetic
    let source = r"
enum MyEnum {
    A = 0,
    B = 1,
    C = 2,
}
const result = MyEnum.A + MyEnum.B;  // OK: enum arithmetic is valid
";
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    // Should not emit TS2362/TS2363 for enum arithmetic
    let error_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 2362 || d.code == 2363)
        .count();
    assert_eq!(
        error_count, 0,
        "Expected no TS2362/TS2363 errors for enum arithmetic, got {error_count}"
    );
}

#[test]
fn test_null_property_access_emits_ts18050() {
    // Test accessing property on null literal - should emit TS18050
    let source = r"
const x = null.toString();  // TS18050: The value 'null' cannot be used here.
";
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    // Should emit TS18050 for null.toString()
    let ts18050_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 18050)
        .count();
    assert!(
        ts18050_count >= 1,
        "Expected at least 1 TS18050 error for null property access, got {ts18050_count}"
    );
}

#[test]
fn test_undefined_property_access_emits_ts18050() {
    // Test accessing property on undefined - should emit TS18050
    let source = r"
const x = undefined.toString();  // TS18050: The value 'undefined' cannot be used here.
";
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    // Should emit TS18050 for undefined.toString()
    let ts18050_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 18050)
        .count();
    assert!(
        ts18050_count >= 1,
        "Expected at least 1 TS18050 error for undefined property access, got {ts18050_count}"
    );
}

#[test]
fn test_string_string_subtraction_emits_ts2362() {
    // Test string - string should emit TS2362
    let source = r#"
const a = "hello";
const b = "world";
const result = a - b;  // TS2362: left-hand side must be number/bigint/any/enum
"#;
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    // Should emit TS2362 for string - string
    let ts2362_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 2362)
        .count();
    assert!(
        ts2362_count >= 1,
        "Expected at least 1 TS2362 error for string subtraction, got {ts2362_count}"
    );
}

#[test]
fn test_never_type_property_access_no_error() {
    // In TSC, property access on `never` returns `never` without any error.
    // `never` is the bottom type — accessing properties on it is valid because
    // it represents unreachable code (exhaustive narrowing patterns).
    let source = r"
function test(x: never) {
    const y = x.toString();
}
";
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    // TSC does NOT emit TS18050 for property access on never — it returns never
    let ts18050_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 18050)
        .count();
    assert_eq!(
        ts18050_count, 0,
        "Property access on never should NOT emit TS18050 (TSC returns never silently)"
    );
}

#[test]
fn test_never_type_call_no_error() {
    // In TSC, calling `never` returns `never` without any error.
    let source = r"
function test(x: never) {
    const y = x();
}
";
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    // TSC does NOT emit TS18050 for calling never — it returns never
    let ts18050_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 18050)
        .count();
    assert_eq!(
        ts18050_count, 0,
        "Calling never should NOT emit TS18050 (TSC returns never silently)"
    );
}

#[test]
fn test_array_object_prototype_properties() {
    // Arrays should have access to Object.prototype properties like constructor,
    // valueOf, hasOwnProperty, etc. (through Array<T> → Object prototype chain)
    let source = r#"
var arr: number[] = [1, 2, 3];
arr.constructor;
arr.valueOf();
arr.hasOwnProperty("length");
"#;
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    // Should have no TS2339 errors — these properties exist on Object.prototype
    let ts2339_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 2339)
        .count();
    assert!(
        ts2339_count == 0,
        "Expected no TS2339 errors for array Object.prototype properties, got {ts2339_count}"
    );
}

#[test]
fn test_shorthand_property_missing_value_emits_ts18004() {
    let source = r"
const make = () => {
    return { arguments };
};
";
    let mut parser = ParserState::new("test.ts".to_string(), source.to_string());
    let root = parser.parse_source_file();

    let mut binder = BinderState::new();
    binder.bind_source_file(parser.get_arena(), root);

    let types = TypeInterner::new();
    let mut checker = CheckerState::new(
        parser.get_arena(),
        &binder,
        &types,
        "test.ts".to_string(),
        crate::context::CheckerOptions::default(),
    );

    checker.check_source_file(root);

    let ts18004_count = checker
        .ctx
        .diagnostics
        .iter()
        .filter(|d| d.code == 18004)
        .count();
    assert!(
        ts18004_count >= 1,
        "Expected TS18004 for missing shorthand property value, got diagnostics: {:?}",
        checker.ctx.diagnostics
    );
}