2014-12-10 19:17:48 +01:00
|
|
|
//// [typeGuardsWithAny.ts]
|
|
|
|
var x: any = { p: 0 };
|
2015-01-08 23:27:39 +01:00
|
|
|
|
2014-12-10 19:17:48 +01:00
|
|
|
if (x instanceof Object) {
|
2015-01-08 23:27:39 +01:00
|
|
|
x.p; // No error, type any unaffected by instanceof type guard
|
2014-12-10 23:25:02 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-01-08 23:27:39 +01:00
|
|
|
x.p; // No error, type any unaffected by instanceof type guard
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof x === "string") {
|
|
|
|
x.p; // Error, type any narrowed by primitive type check
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
x.p; // No error, type unaffected in this branch
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof x === "number") {
|
|
|
|
x.p; // Error, type any narrowed by primitive type check
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
x.p; // No error, type unaffected in this branch
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof x === "boolean") {
|
|
|
|
x.p; // Error, type any narrowed by primitive type check
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
x.p; // No error, type unaffected in this branch
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof x === "object") {
|
|
|
|
x.p; // No error, type any only affected by primitive type check
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
x.p; // No error, type unaffected in this branch
|
2014-12-10 19:17:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//// [typeGuardsWithAny.js]
|
|
|
|
var x = { p: 0 };
|
|
|
|
if (x instanceof Object) {
|
2015-01-08 23:27:39 +01:00
|
|
|
x.p; // No error, type any unaffected by instanceof type guard
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
x.p; // No error, type any unaffected by instanceof type guard
|
|
|
|
}
|
|
|
|
if (typeof x === "string") {
|
|
|
|
x.p; // Error, type any narrowed by primitive type check
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
x.p; // No error, type unaffected in this branch
|
|
|
|
}
|
|
|
|
if (typeof x === "number") {
|
|
|
|
x.p; // Error, type any narrowed by primitive type check
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
x.p; // No error, type unaffected in this branch
|
|
|
|
}
|
|
|
|
if (typeof x === "boolean") {
|
|
|
|
x.p; // Error, type any narrowed by primitive type check
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
x.p; // No error, type unaffected in this branch
|
|
|
|
}
|
|
|
|
if (typeof x === "object") {
|
|
|
|
x.p; // No error, type any only affected by primitive type check
|
2014-12-10 23:25:02 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-01-08 23:27:39 +01:00
|
|
|
x.p; // No error, type unaffected in this branch
|
2014-12-10 19:17:48 +01:00
|
|
|
}
|