Skip to content

Commit 854d448

Browse files
authoredSep 2, 2022
in operator shouldn't narrow {} originating in unknown (#50610)
* 'in' operator shouldn't narrow {} originating in unknown * Add regression test
1 parent 549e61d commit 854d448

File tree

5 files changed

+130
-1
lines changed

5 files changed

+130
-1
lines changed
 

‎src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25150,7 +25150,7 @@ namespace ts {
2515025150

2515125151
function narrowByInKeyword(type: Type, name: __String, assumeTrue: boolean) {
2515225152
if (type.flags & TypeFlags.Union
25153-
|| type.flags & TypeFlags.Object && declaredType !== type
25153+
|| type.flags & TypeFlags.Object && declaredType !== type && !(declaredType === unknownType && isEmptyAnonymousObjectType(type))
2515425154
|| isThisTypeParameter(type)
2515525155
|| type.flags & TypeFlags.Intersection && every((type as IntersectionType).types, t => t.symbol !== globalThisSymbol)) {
2515625156
return filterType(type, t => isTypePresencePossible(t, name, assumeTrue));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [inKeywordAndUnknown.ts]
2+
// Repro from #50531
3+
4+
function f(x: {}, y: unknown) {
5+
if (!("a" in x)) {
6+
return;
7+
}
8+
x; // {}
9+
if (!y) {
10+
return;
11+
}
12+
y; // {}
13+
if (!("a" in y)) {
14+
return;
15+
}
16+
y; // {}
17+
}
18+
19+
20+
//// [inKeywordAndUnknown.js]
21+
"use strict";
22+
// Repro from #50531
23+
function f(x, y) {
24+
if (!("a" in x)) {
25+
return;
26+
}
27+
x; // {}
28+
if (!y) {
29+
return;
30+
}
31+
y; // {}
32+
if (!("a" in y)) {
33+
return;
34+
}
35+
y; // {}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
=== tests/cases/compiler/inKeywordAndUnknown.ts ===
2+
// Repro from #50531
3+
4+
function f(x: {}, y: unknown) {
5+
>f : Symbol(f, Decl(inKeywordAndUnknown.ts, 0, 0))
6+
>x : Symbol(x, Decl(inKeywordAndUnknown.ts, 2, 11))
7+
>y : Symbol(y, Decl(inKeywordAndUnknown.ts, 2, 17))
8+
9+
if (!("a" in x)) {
10+
>x : Symbol(x, Decl(inKeywordAndUnknown.ts, 2, 11))
11+
12+
return;
13+
}
14+
x; // {}
15+
>x : Symbol(x, Decl(inKeywordAndUnknown.ts, 2, 11))
16+
17+
if (!y) {
18+
>y : Symbol(y, Decl(inKeywordAndUnknown.ts, 2, 17))
19+
20+
return;
21+
}
22+
y; // {}
23+
>y : Symbol(y, Decl(inKeywordAndUnknown.ts, 2, 17))
24+
25+
if (!("a" in y)) {
26+
>y : Symbol(y, Decl(inKeywordAndUnknown.ts, 2, 17))
27+
28+
return;
29+
}
30+
y; // {}
31+
>y : Symbol(y, Decl(inKeywordAndUnknown.ts, 2, 17))
32+
}
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
=== tests/cases/compiler/inKeywordAndUnknown.ts ===
2+
// Repro from #50531
3+
4+
function f(x: {}, y: unknown) {
5+
>f : (x: {}, y: unknown) => void
6+
>x : {}
7+
>y : unknown
8+
9+
if (!("a" in x)) {
10+
>!("a" in x) : boolean
11+
>("a" in x) : boolean
12+
>"a" in x : boolean
13+
>"a" : "a"
14+
>x : {}
15+
16+
return;
17+
}
18+
x; // {}
19+
>x : {}
20+
21+
if (!y) {
22+
>!y : boolean
23+
>y : unknown
24+
25+
return;
26+
}
27+
y; // {}
28+
>y : {}
29+
30+
if (!("a" in y)) {
31+
>!("a" in y) : boolean
32+
>("a" in y) : boolean
33+
>"a" in y : boolean
34+
>"a" : "a"
35+
>y : {}
36+
37+
return;
38+
}
39+
y; // {}
40+
>y : {}
41+
}
42+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @strict: true
2+
3+
// Repro from #50531
4+
5+
function f(x: {}, y: unknown) {
6+
if (!("a" in x)) {
7+
return;
8+
}
9+
x; // {}
10+
if (!y) {
11+
return;
12+
}
13+
y; // {}
14+
if (!("a" in y)) {
15+
return;
16+
}
17+
y; // {}
18+
}

0 commit comments

Comments
 (0)
Please sign in to comment.