support 'in' type guard of intersections (#37106)

This commit is contained in:
pizzacat83
2020-04-01 05:37:08 +09:00
committed by GitHub
parent 23b500cadf
commit a46e9aea10
6 changed files with 86 additions and 4 deletions

View File

@@ -20241,7 +20241,7 @@ namespace ts {
}
function narrowByInKeyword(type: Type, literal: LiteralExpression, assumeTrue: boolean) {
if (type.flags & (TypeFlags.Union | TypeFlags.Object) || isThisTypeParameter(type)) {
if (type.flags & (TypeFlags.Union | TypeFlags.Object | TypeFlags.Intersection) || isThisTypeParameter(type)) {
const propName = escapeLeadingUnderscores(literal.text);
return filterType(type, t => isTypePresencePossible(t, propName, assumeTrue));
}

View File

@@ -156,4 +156,13 @@ tests/cases/compiler/inKeywordTypeguard.ts(94,26): error TS2339: Property 'a' do
!!! error TS2339: Property 'a' does not exist on type 'never'.
}
}
}
}
function positiveIntersectionTest(x: { a: string } & { b: string }) {
if ("a" in x) {
let s: string = x.a;
} else {
let n: never = x;
}
}

View File

@@ -95,7 +95,16 @@ class UnreachableCodeDetection {
let y = this.a;
}
}
}
}
function positiveIntersectionTest(x: { a: string } & { b: string }) {
if ("a" in x) {
let s: string = x.a;
} else {
let n: never = x;
}
}
//// [inKeywordTypeguard.js]
var A = /** @class */ (function () {
@@ -228,3 +237,11 @@ var UnreachableCodeDetection = /** @class */ (function () {
};
return UnreachableCodeDetection;
}());
function positiveIntersectionTest(x) {
if ("a" in x) {
var s = x.a;
}
else {
var n = x;
}
}

View File

@@ -239,3 +239,26 @@ class UnreachableCodeDetection {
}
}
}
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))
}
}

View File

@@ -297,3 +297,28 @@ class UnreachableCodeDetection {
}
}
}
function positiveIntersectionTest(x: { a: string } & { b: string }) {
>positiveIntersectionTest : (x: { a: string; } & { b: string; }) => void
>x : { a: string; } & { b: string; }
>a : string
>b : string
if ("a" in x) {
>"a" in x : boolean
>"a" : "a"
>x : { a: string; } & { b: string; }
let s: string = x.a;
>s : string
>x.a : string
>x : { a: string; } & { b: string; }
>a : string
} else {
let n: never = x;
>n : never
>x : never
}
}

View File

@@ -94,4 +94,12 @@ class UnreachableCodeDetection {
let y = this.a;
}
}
}
}
function positiveIntersectionTest(x: { a: string } & { b: string }) {
if ("a" in x) {
let s: string = x.a;
} else {
let n: never = x;
}
}