add tests
This commit is contained in:
parent
cc8f326961
commit
66047c8b18
3 changed files with 80 additions and 0 deletions
|
@ -0,0 +1,23 @@
|
|||
declare function isFooError(x: any): x is { type: 'foo'; dontPanic(); };
|
||||
|
||||
function tryCatch() {
|
||||
try {
|
||||
// do stuff...
|
||||
}
|
||||
catch (err) { // err is implicitly 'any' and cannot be annotated
|
||||
|
||||
if (isFooError(err)) {
|
||||
err.dontPanic(); // OK
|
||||
err.doPanic(); // ERROR: Property 'doPanic' does not exist on type '{...}'
|
||||
}
|
||||
|
||||
else if (err instanceof Error) {
|
||||
err.message;
|
||||
err.massage; // ERROR: Property 'massage' does not exist on type 'Error'
|
||||
}
|
||||
|
||||
else {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
declare var x: any;
|
||||
|
||||
if (x instanceof Function) { // 'any' is not narrowed when target type is 'Function'
|
||||
x();
|
||||
x(1, 2, 3);
|
||||
x("hello!");
|
||||
x.prop;
|
||||
}
|
||||
|
||||
if (x instanceof Object) { // 'any' is not narrowed when target type is 'Object'
|
||||
x.method();
|
||||
x();
|
||||
}
|
||||
|
||||
if (x instanceof Error) { // 'any' is narrowed to types other than 'Function'/'Object'
|
||||
x.message;
|
||||
x.mesage;
|
||||
}
|
||||
|
||||
if (x instanceof Date) {
|
||||
x.getDate();
|
||||
x.getHuors();
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
declare var x: any;
|
||||
declare function isFunction(x): x is Function;
|
||||
declare function isObject(x): x is Object;
|
||||
declare function isAnything(x): x is {};
|
||||
declare function isError(x): x is Error;
|
||||
declare function isDate(x): x is Date;
|
||||
|
||||
|
||||
if (isFunction(x)) { // 'any' is not narrowed when target type is 'Function'
|
||||
x();
|
||||
x(1, 2, 3);
|
||||
x("hello!");
|
||||
x.prop;
|
||||
}
|
||||
|
||||
if (isObject(x)) { // 'any' is not narrowed when target type is 'Object'
|
||||
x.method();
|
||||
x();
|
||||
}
|
||||
|
||||
if (isAnything(x)) { // 'any' is narrowed to types other than 'Function'/'Object' (including {})
|
||||
x.method();
|
||||
x();
|
||||
}
|
||||
|
||||
if (isError(x)) {
|
||||
x.message;
|
||||
x.mesage;
|
||||
}
|
||||
|
||||
if (isDate(x)) {
|
||||
x.getDate();
|
||||
x.getHuors();
|
||||
}
|
Loading…
Reference in a new issue