From b3b4c34b609aeac08230251a9c68aa17d8eaf036 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 1 Aug 2016 07:04:43 -0700 Subject: [PATCH] Add additional tests --- .../compiler/narrowingByDiscriminantInLoop.ts | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/tests/cases/compiler/narrowingByDiscriminantInLoop.ts b/tests/cases/compiler/narrowingByDiscriminantInLoop.ts index d9519c57a62..f394018d2ce 100644 --- a/tests/cases/compiler/narrowingByDiscriminantInLoop.ts +++ b/tests/cases/compiler/narrowingByDiscriminantInLoop.ts @@ -1,3 +1,5 @@ +// @strictNullChecks: true + // Repro from #9977 type IDLMemberTypes = OperationMemberType | ConstantMemberType; @@ -12,7 +14,7 @@ interface InterfaceType { interface OperationMemberType { type: "operation"; - idlType: IDLTypeDescription | null; + idlType: IDLTypeDescription; } interface ConstantMemberType { @@ -47,4 +49,39 @@ function foo(memberType: IDLMemberTypes) { else if (memberType.type === "operation") { memberType.idlType.origin; // string } +} + +// Repro for issue similar to #8383 + +interface A { + kind: true; + prop: { a: string; }; +} + +interface B { + kind: false; + prop: { b: string; } +} + +function f1(x: A | B) { + while (true) { + x.prop; + if (x.kind === true) { + x.prop.a; + } + if (x.kind === false) { + x.prop.b; + } + } +} + +function f2(x: A | B) { + while (true) { + if (x.kind) { + x.prop.a; + } + if (!x.kind) { + x.prop.b; + } + } } \ No newline at end of file