File tree Expand file tree Collapse file tree 3 files changed +80
-0
lines changed
tests/cases/conformance/types/any Expand file tree Collapse file tree 3 files changed +80
-0
lines changed Original file line number Diff line number Diff line change
1
+ declare function isFooError ( x : any ) : x is { type : 'foo' ; dontPanic ( ) ; } ;
2
+
3
+ function tryCatch ( ) {
4
+ try {
5
+ // do stuff...
6
+ }
7
+ catch ( err ) { // err is implicitly 'any' and cannot be annotated
8
+
9
+ if ( isFooError ( err ) ) {
10
+ err . dontPanic ( ) ; // OK
11
+ err . doPanic ( ) ; // ERROR: Property 'doPanic' does not exist on type '{...}'
12
+ }
13
+
14
+ else if ( err instanceof Error ) {
15
+ err . message ;
16
+ err . massage ; // ERROR: Property 'massage' does not exist on type 'Error'
17
+ }
18
+
19
+ else {
20
+ throw err ;
21
+ }
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ declare var x : any ;
2
+
3
+ if ( x instanceof Function ) { // 'any' is not narrowed when target type is 'Function'
4
+ x ( ) ;
5
+ x ( 1 , 2 , 3 ) ;
6
+ x ( "hello!" ) ;
7
+ x . prop ;
8
+ }
9
+
10
+ if ( x instanceof Object ) { // 'any' is not narrowed when target type is 'Object'
11
+ x . method ( ) ;
12
+ x ( ) ;
13
+ }
14
+
15
+ if ( x instanceof Error ) { // 'any' is narrowed to types other than 'Function'/'Object'
16
+ x . message ;
17
+ x . mesage ;
18
+ }
19
+
20
+ if ( x instanceof Date ) {
21
+ x . getDate ( ) ;
22
+ x . getHuors ( ) ;
23
+ }
Original file line number Diff line number Diff line change
1
+ declare var x : any ;
2
+ declare function isFunction ( x ) : x is Function ;
3
+ declare function isObject ( x ) : x is Object ;
4
+ declare function isAnything ( x ) : x is { } ;
5
+ declare function isError ( x ) : x is Error ;
6
+ declare function isDate ( x ) : x is Date ;
7
+
8
+
9
+ if ( isFunction ( x ) ) { // 'any' is not narrowed when target type is 'Function'
10
+ x ( ) ;
11
+ x ( 1 , 2 , 3 ) ;
12
+ x ( "hello!" ) ;
13
+ x . prop ;
14
+ }
15
+
16
+ if ( isObject ( x ) ) { // 'any' is not narrowed when target type is 'Object'
17
+ x . method ( ) ;
18
+ x ( ) ;
19
+ }
20
+
21
+ if ( isAnything ( x ) ) { // 'any' is narrowed to types other than 'Function'/'Object' (including {})
22
+ x . method ( ) ;
23
+ x ( ) ;
24
+ }
25
+
26
+ if ( isError ( x ) ) {
27
+ x . message ;
28
+ x . mesage ;
29
+ }
30
+
31
+ if ( isDate ( x ) ) {
32
+ x . getDate ( ) ;
33
+ x . getHuors ( ) ;
34
+ }
You can’t perform that action at this time.
0 commit comments