TypeScript/tests/cases/conformance/expressions/typeGuards/typeGuardOfFromPropNameInUnionType.ts
2017-12-07 10:56:33 -08:00

105 lines
2.1 KiB
TypeScript

class A { a: string; }
class B { b: number; }
class C { b: Object; }
class D { a: Date; }
function namedClasses(x: A | B) {
if ("a" in x) {
x.a = "1";
} else {
x.b = 1;
}
}
function multipleClasses(x: A | B | C | D) {
if ("a" in x) {
let y: string | Date = x.a;
} else {
let z: number | Object = x.b;
}
}
function anonymousClasses(x: { a: string; } | { b: number; }) {
if ("a" in x) {
let y: string = x.a;
} else {
let z: number = x.b;
}
}
class AWithOptionalProp { a?: string; }
class BWithOptionalProp { b?: string; }
function positiveTestClassesWithOptionalProperties(x: AWithOptionalProp | BWithOptionalProp) {
if ("a" in x) {
x.a = "1";
} else {
const y: string = x instanceof AWithOptionalProp
? x.a
: x.b
}
}
function inParenthesizedExpression(x: A | B) {
if ("a" in (x)) {
let y: string = x.a;
} else {
let z: number = x.b;
}
}
class ClassWithUnionProp { prop: A | B; }
function inProperty(x: ClassWithUnionProp) {
if ("a" in x.prop) {
let y: string = x.prop.a;
} else {
let z: number = x.prop.b;
}
}
class NestedClassWithProp { outer: ClassWithUnionProp; }
function innestedProperty(x: NestedClassWithProp) {
if ("a" in x.outer.prop) {
let y: string = x.outer.prop.a;
} else {
let z: number = x.outer.prop.b;
}
}
class InMemberOfClass {
protected prop: A | B;
inThis() {
if ("a" in this.prop) {
let y: string = this.prop.a;
} else {
let z: number = this.prop.b;
}
}
}
// added for completeness
class SelfAssert {
a: string;
inThis() {
if ("a" in this) {
let y: string = this.a;
} else {
}
}
}
interface Indexed {
[s: string]: any;
}
function f(i: Indexed) {
if ("a" in i) {
return i.a;
}
else if ("b" in i) {
return i.b;
}
return "c" in i && i.c;
}