TypeScript/tests/baselines/reference/typeGuardsWithAny.js

72 lines
1.7 KiB
TypeScript
Raw Normal View History

2014-12-10 19:17:48 +01:00
//// [typeGuardsWithAny.ts]
var x: any = { p: 0 };
2014-12-10 19:17:48 +01:00
if (x instanceof Object) {
x.p; // No error, type any unaffected by instanceof type guard
2014-12-10 23:25:02 +01:00
}
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
}
else {
x.p; // No error, type unaffected in this branch
2014-12-10 19:17:48 +01:00
}
//// [typeGuardsWithAny.js]
var x = { p: 0 };
2014-12-10 19:17:48 +01:00
if (x instanceof Object) {
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 {
x.p; // No error, type unaffected in this branch
2014-12-10 19:17:48 +01:00
}