File tree 2 files changed +88
-0
lines changed
tests/cases/conformance/types/stringLiteral
2 files changed +88
-0
lines changed Original file line number Diff line number Diff line change
1
+ // @declaration : true
2
+
3
+ type Kind = "A" | "B"
4
+
5
+ interface Entity {
6
+ kind : Kind ;
7
+ }
8
+
9
+ interface A extends Entity {
10
+ kind : "A" ;
11
+ a : number ;
12
+ }
13
+
14
+ interface B extends Entity {
15
+ kind : "B" ;
16
+ b : string ;
17
+ }
18
+
19
+ function hasKind ( entity : Entity , kind : "A" ) : entity is A ;
20
+ function hasKind ( entity : Entity , kind : "B" ) : entity is B ;
21
+ function hasKind ( entity : Entity , kind : Kind ) : entity is ( A | B ) {
22
+ return entity . kind === kind ;
23
+ }
24
+
25
+ let x : A = {
26
+ kind : "A" ,
27
+ a : 100 ,
28
+ }
29
+
30
+ if ( hasKind ( x , "A" ) ) {
31
+ let a = x ;
32
+ }
33
+ else {
34
+ let b = x ;
35
+ }
36
+
37
+ if ( ! hasKind ( x , "B" ) ) {
38
+ let c = x ;
39
+ }
40
+ else {
41
+ let d = x ;
42
+ }
Original file line number Diff line number Diff line change
1
+ // @declaration : true
2
+
3
+ type Kind = "A" | "B"
4
+
5
+ interface Entity {
6
+ kind : Kind ;
7
+ }
8
+
9
+ interface A extends Entity {
10
+ kind : "A" ;
11
+ a : number ;
12
+ }
13
+
14
+ interface B extends Entity {
15
+ kind : "B" ;
16
+ b : string ;
17
+ }
18
+
19
+ // Currently (2015-12-14), we write '"A" | "A"' and '"B" | "B"' to avoid
20
+ // interpreting respective overloads as "specialized" signatures.
21
+ // That way, we can avoid the need to look for a compatible overload
22
+ // signature and simply check compatibility with the implementation.
23
+ function hasKind ( entity : Entity , kind : "A" | "A" ) : entity is A ;
24
+ function hasKind ( entity : Entity , kind : "B" | "B" ) : entity is B ;
25
+ function hasKind ( entity : Entity , kind : Kind ) : entity is Entity {
26
+ return entity . kind === kind ;
27
+ }
28
+
29
+ let x : A = {
30
+ kind : "A" ,
31
+ a : 100 ,
32
+ }
33
+
34
+ if ( hasKind ( x , "A" ) ) {
35
+ let a = x ;
36
+ }
37
+ else {
38
+ let b = x ;
39
+ }
40
+
41
+ if ( ! hasKind ( x , "B" ) ) {
42
+ let c = x ;
43
+ }
44
+ else {
45
+ let d = x ;
46
+ }
You can’t perform that action at this time.
0 commit comments