Skip to content

Commit 4eaee73

Browse files
committed
Add test for invalid property access in unions
1 parent 59d027b commit 4eaee73

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
tests/cases/compiler/unionPropertyExistance.ts(24,4): error TS2339: Property 'onlyInB' does not exist on type 'AB'.
2+
Property 'onlyInB' does not exist on type 'A'.
3+
tests/cases/compiler/unionPropertyExistance.ts(27,5): error TS2339: Property 'notInC' does not exist on type 'ABC'.
4+
Property 'notInC' does not exist on type 'C'.
5+
tests/cases/compiler/unionPropertyExistance.ts(28,4): error TS2339: Property 'notInB' does not exist on type 'AB'.
6+
Property 'notInB' does not exist on type 'B'.
7+
tests/cases/compiler/unionPropertyExistance.ts(29,5): error TS2339: Property 'notInB' does not exist on type 'ABC'.
8+
Property 'notInB' does not exist on type 'B'.
9+
tests/cases/compiler/unionPropertyExistance.ts(32,5): error TS2339: Property 'inNone' does not exist on type 'ABC'.
10+
Property 'inNone' does not exist on type 'A'.
11+
12+
13+
==== tests/cases/compiler/unionPropertyExistance.ts (5 errors) ====
14+
interface A {
15+
inAll: string;
16+
notInB: string;
17+
notInC: string;
18+
}
19+
20+
interface B {
21+
inAll: boolean;
22+
onlyInB: number;
23+
notInC: string;
24+
}
25+
26+
interface C {
27+
inAll: number;
28+
notInB: string;
29+
}
30+
31+
type AB = A | B;
32+
type ABC = C | AB;
33+
34+
var ab: AB;
35+
var abc: ABC;
36+
37+
ab.onlyInB;
38+
~~~~~~~
39+
!!! error TS2339: Property 'onlyInB' does not exist on type 'AB'.
40+
!!! error TS2339: Property 'onlyInB' does not exist on type 'A'.
41+
42+
ab.notInC; // Ok
43+
abc.notInC;
44+
~~~~~~
45+
!!! error TS2339: Property 'notInC' does not exist on type 'ABC'.
46+
!!! error TS2339: Property 'notInC' does not exist on type 'C'.
47+
ab.notInB;
48+
~~~~~~
49+
!!! error TS2339: Property 'notInB' does not exist on type 'AB'.
50+
!!! error TS2339: Property 'notInB' does not exist on type 'B'.
51+
abc.notInB;
52+
~~~~~~
53+
!!! error TS2339: Property 'notInB' does not exist on type 'ABC'.
54+
!!! error TS2339: Property 'notInB' does not exist on type 'B'.
55+
56+
abc.inAll; // Ok
57+
abc.inNone;
58+
~~~~~~
59+
!!! error TS2339: Property 'inNone' does not exist on type 'ABC'.
60+
!!! error TS2339: Property 'inNone' does not exist on type 'A'.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//// [unionPropertyExistance.ts]
2+
interface A {
3+
inAll: string;
4+
notInB: string;
5+
notInC: string;
6+
}
7+
8+
interface B {
9+
inAll: boolean;
10+
onlyInB: number;
11+
notInC: string;
12+
}
13+
14+
interface C {
15+
inAll: number;
16+
notInB: string;
17+
}
18+
19+
type AB = A | B;
20+
type ABC = C | AB;
21+
22+
var ab: AB;
23+
var abc: ABC;
24+
25+
ab.onlyInB;
26+
27+
ab.notInC; // Ok
28+
abc.notInC;
29+
ab.notInB;
30+
abc.notInB;
31+
32+
abc.inAll; // Ok
33+
abc.inNone;
34+
35+
//// [unionPropertyExistance.js]
36+
var ab;
37+
var abc;
38+
ab.onlyInB;
39+
ab.notInC; // Ok
40+
abc.notInC;
41+
ab.notInB;
42+
abc.notInB;
43+
abc.inAll; // Ok
44+
abc.inNone;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
interface A {
2+
inAll: string;
3+
notInB: string;
4+
notInC: string;
5+
}
6+
7+
interface B {
8+
inAll: boolean;
9+
onlyInB: number;
10+
notInC: string;
11+
}
12+
13+
interface C {
14+
inAll: number;
15+
notInB: string;
16+
}
17+
18+
type AB = A | B;
19+
type ABC = C | AB;
20+
21+
var ab: AB;
22+
var abc: ABC;
23+
24+
ab.onlyInB;
25+
26+
ab.notInC; // Ok
27+
abc.notInC;
28+
ab.notInB;
29+
abc.notInB;
30+
31+
abc.inAll; // Ok
32+
abc.inNone;

0 commit comments

Comments
 (0)