mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 12:51:30 -05:00
add tests
This commit is contained in:
@@ -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();
|
||||
}
|
||||
Reference in New Issue
Block a user