mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-30 01:04:49 -05:00
support 'in' type guard of intersections (#37106)
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user