TypeScript/tests/baselines/reference/inKeywordTypeguard.symbols
Jack Williams 3d6650eb4f
Fix #38608 (#38610)
* Fix #38608

* Work for narrowed non-unions

* Add comment
2021-03-11 07:32:49 -08:00

380 lines
14 KiB
Plaintext

=== tests/cases/compiler/inKeywordTypeguard.ts ===
class A { a: string; }
>A : Symbol(A, Decl(inKeywordTypeguard.ts, 0, 0))
>a : Symbol(A.a, Decl(inKeywordTypeguard.ts, 0, 9))
class B { b: string; }
>B : Symbol(B, Decl(inKeywordTypeguard.ts, 0, 22))
>b : Symbol(B.b, Decl(inKeywordTypeguard.ts, 1, 9))
function negativeClassesTest(x: A | B) {
>negativeClassesTest : Symbol(negativeClassesTest, Decl(inKeywordTypeguard.ts, 1, 22))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 3, 29))
>A : Symbol(A, Decl(inKeywordTypeguard.ts, 0, 0))
>B : Symbol(B, Decl(inKeywordTypeguard.ts, 0, 22))
if ("a" in x) {
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 3, 29))
x.b = "1";
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 3, 29))
} else {
x.a = "1";
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 3, 29))
}
}
function positiveClassesTest(x: A | B) {
>positiveClassesTest : Symbol(positiveClassesTest, Decl(inKeywordTypeguard.ts, 9, 1))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 11, 29))
>A : Symbol(A, Decl(inKeywordTypeguard.ts, 0, 0))
>B : Symbol(B, Decl(inKeywordTypeguard.ts, 0, 22))
if ("a" in x) {
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 11, 29))
x.b = "1";
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 11, 29))
} else {
x.a = "1";
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 11, 29))
}
}
class AWithOptionalProp { a?: string; }
>AWithOptionalProp : Symbol(AWithOptionalProp, Decl(inKeywordTypeguard.ts, 17, 1))
>a : Symbol(AWithOptionalProp.a, Decl(inKeywordTypeguard.ts, 19, 25))
class BWithOptionalProp { b?: string; }
>BWithOptionalProp : Symbol(BWithOptionalProp, Decl(inKeywordTypeguard.ts, 19, 39))
>b : Symbol(BWithOptionalProp.b, Decl(inKeywordTypeguard.ts, 20, 25))
function positiveTestClassesWithOptionalProperties(x: AWithOptionalProp | BWithOptionalProp) {
>positiveTestClassesWithOptionalProperties : Symbol(positiveTestClassesWithOptionalProperties, Decl(inKeywordTypeguard.ts, 20, 39))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 22, 51))
>AWithOptionalProp : Symbol(AWithOptionalProp, Decl(inKeywordTypeguard.ts, 17, 1))
>BWithOptionalProp : Symbol(BWithOptionalProp, Decl(inKeywordTypeguard.ts, 19, 39))
if ("a" in x) {
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 22, 51))
x.a = "1";
>x.a : Symbol(AWithOptionalProp.a, Decl(inKeywordTypeguard.ts, 19, 25))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 22, 51))
>a : Symbol(AWithOptionalProp.a, Decl(inKeywordTypeguard.ts, 19, 25))
} else {
x.b = "1";
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 22, 51))
}
}
class AWithMethod {
>AWithMethod : Symbol(AWithMethod, Decl(inKeywordTypeguard.ts, 28, 1))
a(): string { return ""; }
>a : Symbol(AWithMethod.a, Decl(inKeywordTypeguard.ts, 30, 19))
}
class BWithMethod {
>BWithMethod : Symbol(BWithMethod, Decl(inKeywordTypeguard.ts, 32, 1))
b(): string { return ""; }
>b : Symbol(BWithMethod.b, Decl(inKeywordTypeguard.ts, 34, 19))
}
function negativeTestClassesWithMembers(x: AWithMethod | BWithMethod) {
>negativeTestClassesWithMembers : Symbol(negativeTestClassesWithMembers, Decl(inKeywordTypeguard.ts, 36, 1))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 38, 40))
>AWithMethod : Symbol(AWithMethod, Decl(inKeywordTypeguard.ts, 28, 1))
>BWithMethod : Symbol(BWithMethod, Decl(inKeywordTypeguard.ts, 32, 1))
if ("a" in x) {
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 38, 40))
x.a();
>x.a : Symbol(AWithMethod.a, Decl(inKeywordTypeguard.ts, 30, 19))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 38, 40))
>a : Symbol(AWithMethod.a, Decl(inKeywordTypeguard.ts, 30, 19))
x.b();
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 38, 40))
} else {
}
}
function negativeTestClassesWithMemberMissingInBothClasses(x: AWithMethod | BWithMethod) {
>negativeTestClassesWithMemberMissingInBothClasses : Symbol(negativeTestClassesWithMemberMissingInBothClasses, Decl(inKeywordTypeguard.ts, 44, 1))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 46, 59))
>AWithMethod : Symbol(AWithMethod, Decl(inKeywordTypeguard.ts, 28, 1))
>BWithMethod : Symbol(BWithMethod, Decl(inKeywordTypeguard.ts, 32, 1))
if ("c" in x) {
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 46, 59))
x.a();
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 46, 59))
x.b();
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 46, 59))
} else {
x.a();
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 46, 59))
x.b();
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 46, 59))
}
}
class C { a: string; }
>C : Symbol(C, Decl(inKeywordTypeguard.ts, 54, 1))
>a : Symbol(C.a, Decl(inKeywordTypeguard.ts, 56, 9))
class D { a: string; }
>D : Symbol(D, Decl(inKeywordTypeguard.ts, 56, 22))
>a : Symbol(D.a, Decl(inKeywordTypeguard.ts, 57, 9))
function negativeMultipleClassesTest(x: A | B | C | D) {
>negativeMultipleClassesTest : Symbol(negativeMultipleClassesTest, Decl(inKeywordTypeguard.ts, 57, 22))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 59, 37))
>A : Symbol(A, Decl(inKeywordTypeguard.ts, 0, 0))
>B : Symbol(B, Decl(inKeywordTypeguard.ts, 0, 22))
>C : Symbol(C, Decl(inKeywordTypeguard.ts, 54, 1))
>D : Symbol(D, Decl(inKeywordTypeguard.ts, 56, 22))
if ("a" in x) {
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 59, 37))
x.b = "1";
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 59, 37))
} else {
x.a = "1";
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 59, 37))
}
}
class ClassWithUnionProp { prop: A | B }
>ClassWithUnionProp : Symbol(ClassWithUnionProp, Decl(inKeywordTypeguard.ts, 65, 1))
>prop : Symbol(ClassWithUnionProp.prop, Decl(inKeywordTypeguard.ts, 67, 26))
>A : Symbol(A, Decl(inKeywordTypeguard.ts, 0, 0))
>B : Symbol(B, Decl(inKeywordTypeguard.ts, 0, 22))
function negativePropTest(x: ClassWithUnionProp) {
>negativePropTest : Symbol(negativePropTest, Decl(inKeywordTypeguard.ts, 67, 40))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 69, 26))
>ClassWithUnionProp : Symbol(ClassWithUnionProp, Decl(inKeywordTypeguard.ts, 65, 1))
if ("a" in x.prop) {
>x.prop : Symbol(ClassWithUnionProp.prop, Decl(inKeywordTypeguard.ts, 67, 26))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 69, 26))
>prop : Symbol(ClassWithUnionProp.prop, Decl(inKeywordTypeguard.ts, 67, 26))
let y: string = x.prop.b;
>y : Symbol(y, Decl(inKeywordTypeguard.ts, 71, 11))
>x.prop : Symbol(ClassWithUnionProp.prop, Decl(inKeywordTypeguard.ts, 67, 26))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 69, 26))
>prop : Symbol(ClassWithUnionProp.prop, Decl(inKeywordTypeguard.ts, 67, 26))
} else {
let z: string = x.prop.a;
>z : Symbol(z, Decl(inKeywordTypeguard.ts, 73, 11))
>x.prop : Symbol(ClassWithUnionProp.prop, Decl(inKeywordTypeguard.ts, 67, 26))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 69, 26))
>prop : Symbol(ClassWithUnionProp.prop, Decl(inKeywordTypeguard.ts, 67, 26))
}
}
class NegativeClassTest {
>NegativeClassTest : Symbol(NegativeClassTest, Decl(inKeywordTypeguard.ts, 75, 1))
protected prop: A | B;
>prop : Symbol(NegativeClassTest.prop, Decl(inKeywordTypeguard.ts, 77, 25))
>A : Symbol(A, Decl(inKeywordTypeguard.ts, 0, 0))
>B : Symbol(B, Decl(inKeywordTypeguard.ts, 0, 22))
inThis() {
>inThis : Symbol(NegativeClassTest.inThis, Decl(inKeywordTypeguard.ts, 78, 26))
if ("a" in this.prop) {
>this.prop : Symbol(NegativeClassTest.prop, Decl(inKeywordTypeguard.ts, 77, 25))
>this : Symbol(NegativeClassTest, Decl(inKeywordTypeguard.ts, 75, 1))
>prop : Symbol(NegativeClassTest.prop, Decl(inKeywordTypeguard.ts, 77, 25))
let z: number = this.prop.b;
>z : Symbol(z, Decl(inKeywordTypeguard.ts, 81, 15))
>this.prop : Symbol(NegativeClassTest.prop, Decl(inKeywordTypeguard.ts, 77, 25))
>this : Symbol(NegativeClassTest, Decl(inKeywordTypeguard.ts, 75, 1))
>prop : Symbol(NegativeClassTest.prop, Decl(inKeywordTypeguard.ts, 77, 25))
} else {
let y: string = this.prop.a;
>y : Symbol(y, Decl(inKeywordTypeguard.ts, 83, 15))
>this.prop : Symbol(NegativeClassTest.prop, Decl(inKeywordTypeguard.ts, 77, 25))
>this : Symbol(NegativeClassTest, Decl(inKeywordTypeguard.ts, 75, 1))
>prop : Symbol(NegativeClassTest.prop, Decl(inKeywordTypeguard.ts, 77, 25))
}
}
}
class UnreachableCodeDetection {
>UnreachableCodeDetection : Symbol(UnreachableCodeDetection, Decl(inKeywordTypeguard.ts, 86, 1))
a: string;
>a : Symbol(UnreachableCodeDetection.a, Decl(inKeywordTypeguard.ts, 88, 32))
inThis() {
>inThis : Symbol(UnreachableCodeDetection.inThis, Decl(inKeywordTypeguard.ts, 89, 14))
if ("a" in this) {
>this : Symbol(UnreachableCodeDetection, Decl(inKeywordTypeguard.ts, 86, 1))
} else {
let y = this.a;
>y : Symbol(y, Decl(inKeywordTypeguard.ts, 93, 15))
}
}
}
function positiveIntersectionTest(x: { a: string } & { b: string }) {
>positiveIntersectionTest : Symbol(positiveIntersectionTest, Decl(inKeywordTypeguard.ts, 96, 1))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 98, 34))
>a : Symbol(a, Decl(inKeywordTypeguard.ts, 98, 38))
>b : Symbol(b, Decl(inKeywordTypeguard.ts, 98, 54))
if ("a" in x) {
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 98, 34))
let s: string = x.a;
>s : Symbol(s, Decl(inKeywordTypeguard.ts, 100, 11))
>x.a : Symbol(a, Decl(inKeywordTypeguard.ts, 98, 38))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 98, 34))
>a : Symbol(a, Decl(inKeywordTypeguard.ts, 98, 38))
} else {
let n: never = x;
>n : Symbol(n, Decl(inKeywordTypeguard.ts, 102, 11))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 98, 34))
}
}
// Repro from #38608
declare const error: Error;
>error : Symbol(error, Decl(inKeywordTypeguard.ts, 107, 13))
>Error : Symbol(Error, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
if ('extra' in error) {
>error : Symbol(error, Decl(inKeywordTypeguard.ts, 107, 13))
error // Still Error
>error : Symbol(error, Decl(inKeywordTypeguard.ts, 107, 13))
} else {
error // Error
>error : Symbol(error, Decl(inKeywordTypeguard.ts, 107, 13))
}
function narrowsToNever(x: { l: number } | { r: number }) {
>narrowsToNever : Symbol(narrowsToNever, Decl(inKeywordTypeguard.ts, 112, 1))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 114, 24))
>l : Symbol(l, Decl(inKeywordTypeguard.ts, 114, 28))
>r : Symbol(r, Decl(inKeywordTypeguard.ts, 114, 44))
let v: number;
>v : Symbol(v, Decl(inKeywordTypeguard.ts, 115, 7))
if ("l" in x) {
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 114, 24))
v = x.l;
>v : Symbol(v, Decl(inKeywordTypeguard.ts, 115, 7))
>x.l : Symbol(l, Decl(inKeywordTypeguard.ts, 114, 28))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 114, 24))
>l : Symbol(l, Decl(inKeywordTypeguard.ts, 114, 28))
}
else if ("r" in x) {
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 114, 24))
v = x.r;
>v : Symbol(v, Decl(inKeywordTypeguard.ts, 115, 7))
>x.r : Symbol(r, Decl(inKeywordTypeguard.ts, 114, 44))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 114, 24))
>r : Symbol(r, Decl(inKeywordTypeguard.ts, 114, 44))
}
else {
v = x
>v : Symbol(v, Decl(inKeywordTypeguard.ts, 115, 7))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 114, 24))
}
return v;
>v : Symbol(v, Decl(inKeywordTypeguard.ts, 115, 7))
}
type AOrB = { aProp: number } | { bProp: number };
>AOrB : Symbol(AOrB, Decl(inKeywordTypeguard.ts, 126, 1))
>aProp : Symbol(aProp, Decl(inKeywordTypeguard.ts, 128, 13))
>bProp : Symbol(bProp, Decl(inKeywordTypeguard.ts, 128, 33))
declare function isAOrB(x: unknown): x is AOrB;
>isAOrB : Symbol(isAOrB, Decl(inKeywordTypeguard.ts, 128, 50))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 129, 24))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 129, 24))
>AOrB : Symbol(AOrB, Decl(inKeywordTypeguard.ts, 126, 1))
declare var x: unknown;
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 131, 11))
if (isAOrB(x)) {
>isAOrB : Symbol(isAOrB, Decl(inKeywordTypeguard.ts, 128, 50))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 131, 11))
if ("aProp" in x) {
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 131, 11))
x.aProp;
>x.aProp : Symbol(aProp, Decl(inKeywordTypeguard.ts, 128, 13))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 131, 11))
>aProp : Symbol(aProp, Decl(inKeywordTypeguard.ts, 128, 13))
}
else if ("bProp" in x) {
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 131, 11))
x.bProp;
>x.bProp : Symbol(bProp, Decl(inKeywordTypeguard.ts, 128, 33))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 131, 11))
>bProp : Symbol(bProp, Decl(inKeywordTypeguard.ts, 128, 33))
}
// x is never because of the type predicate from unknown
else if ("cProp" in x) {
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 131, 11))
const _never: never = x;
>_never : Symbol(_never, Decl(inKeywordTypeguard.ts, 141, 13))
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 131, 11))
}
}
function negativeIntersectionTest() {
>negativeIntersectionTest : Symbol(negativeIntersectionTest, Decl(inKeywordTypeguard.ts, 143, 1))
if ("ontouchstart" in window) {
>window : Symbol(window, Decl(lib.dom.d.ts, --, --))
window.ontouchstart
>window.ontouchstart : Symbol(ontouchstart, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --))
>window : Symbol(window, Decl(lib.dom.d.ts, --, --))
>ontouchstart : Symbol(ontouchstart, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --))
} else {
window.ontouchstart
>window.ontouchstart : Symbol(ontouchstart, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --))
>window : Symbol(window, Decl(lib.dom.d.ts, --, --))
>ontouchstart : Symbol(ontouchstart, Decl(lib.dom.d.ts, --, --), Decl(lib.dom.d.ts, --, --))
}
}