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
4 changes: 3 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21430,7 +21430,9 @@ 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)
|| isThisTypeParameter(type)
|| type.flags & TypeFlags.Intersection && every((type as IntersectionType).types, t => t.symbol !== globalThisSymbol)) {
const propName = escapeLeadingUnderscores(literal.text);
return filterType(type, t => isTypePresencePossible(t, propName, assumeTrue));
}
Expand Down
12 changes: 8 additions & 4 deletions tests/baselines/reference/inKeywordTypeguard.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ tests/cases/compiler/inKeywordTypeguard.ts(74,32): error TS2339: Property 'a' do
tests/cases/compiler/inKeywordTypeguard.ts(82,39): error TS2339: Property 'b' does not exist on type 'A'.
tests/cases/compiler/inKeywordTypeguard.ts(84,39): error TS2339: Property 'a' does not exist on type 'B'.
tests/cases/compiler/inKeywordTypeguard.ts(94,26): error TS2339: Property 'a' does not exist on type 'never'.
tests/cases/compiler/inKeywordTypeguard.ts(103,13): error TS2322: Type '{ a: string; } & { b: string; }' is not assignable to type 'never'.


==== tests/cases/compiler/inKeywordTypeguard.ts (18 errors) ====
==== tests/cases/compiler/inKeywordTypeguard.ts (17 errors) ====
class A { a: string; }
class B { b: string; }

Expand Down Expand Up @@ -164,8 +163,13 @@ tests/cases/compiler/inKeywordTypeguard.ts(103,13): error TS2322: Type '{ a: str
let s: string = x.a;
} else {
let n: never = x;
~
!!! error TS2322: Type '{ a: string; } & { b: string; }' is not assignable to type 'never'.
}
}
function negativeIntersectionTest() {
if ("ontouchstart" in window) {
window.ontouchstart
} else {
window.ontouchstart
}
}

15 changes: 15 additions & 0 deletions tests/baselines/reference/inKeywordTypeguard.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ function positiveIntersectionTest(x: { a: string } & { b: string }) {
let n: never = x;
}
}
function negativeIntersectionTest() {
if ("ontouchstart" in window) {
window.ontouchstart
} else {
window.ontouchstart
}
}


//// [inKeywordTypeguard.js]
Expand Down Expand Up @@ -245,3 +252,11 @@ function positiveIntersectionTest(x) {
var n = x;
}
}
function negativeIntersectionTest() {
if ("ontouchstart" in window) {
window.ontouchstart;
}
else {
window.ontouchstart;
}
}
18 changes: 18 additions & 0 deletions tests/baselines/reference/inKeywordTypeguard.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,22 @@ function positiveIntersectionTest(x: { a: string } & { b: string }) {
>x : Symbol(x, Decl(inKeywordTypeguard.ts, 98, 34))
}
}
function negativeIntersectionTest() {
>negativeIntersectionTest : Symbol(negativeIntersectionTest, Decl(inKeywordTypeguard.ts, 104, 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, --, --))
}
}

22 changes: 21 additions & 1 deletion tests/baselines/reference/inKeywordTypeguard.types
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,27 @@ function positiveIntersectionTest(x: { a: string } & { b: string }) {
} else {
let n: never = x;
>n : never
>x : { a: string; } & { b: string; }
>x : never
}
}
function negativeIntersectionTest() {
>negativeIntersectionTest : () => void

if ("ontouchstart" in window) {
>"ontouchstart" in window : boolean
>"ontouchstart" : "ontouchstart"
>window : Window & typeof globalThis

window.ontouchstart
>window.ontouchstart : ((this: GlobalEventHandlers, ev: TouchEvent) => any) & ((this: Window, ev: TouchEvent) => any)
>window : Window & typeof globalThis
>ontouchstart : ((this: GlobalEventHandlers, ev: TouchEvent) => any) & ((this: Window, ev: TouchEvent) => any)

} else {
window.ontouchstart
>window.ontouchstart : ((this: GlobalEventHandlers, ev: TouchEvent) => any) & ((this: Window, ev: TouchEvent) => any)
>window : Window & typeof globalThis
>ontouchstart : ((this: GlobalEventHandlers, ev: TouchEvent) => any) & ((this: Window, ev: TouchEvent) => any)
}
}

7 changes: 7 additions & 0 deletions tests/cases/compiler/inKeywordTypeguard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,10 @@ function positiveIntersectionTest(x: { a: string } & { b: string }) {
let n: never = x;
}
}
function negativeIntersectionTest() {
if ("ontouchstart" in window) {
window.ontouchstart
} else {
window.ontouchstart
}
}