Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20028,7 +20028,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));
}
Expand Down
11 changes: 10 additions & 1 deletion tests/baselines/reference/inKeywordTypeguard.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

19 changes: 18 additions & 1 deletion tests/baselines/reference/inKeywordTypeguard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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;
}
}
23 changes: 23 additions & 0 deletions tests/baselines/reference/inKeywordTypeguard.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

25 changes: 25 additions & 0 deletions tests/baselines/reference/inKeywordTypeguard.types
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

10 changes: 9 additions & 1 deletion tests/cases/compiler/inKeywordTypeguard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}