mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-04 21:53:42 -06:00
in operator shouldn't narrow {} originating in unknown (#50610)
* 'in' operator shouldn't narrow {} originating in unknown
* Add regression test
This commit is contained in:
parent
549e61d0af
commit
854d448e5c
@ -25150,7 +25150,7 @@ namespace ts {
|
||||
|
||||
function narrowByInKeyword(type: Type, name: __String, assumeTrue: boolean) {
|
||||
if (type.flags & TypeFlags.Union
|
||||
|| type.flags & TypeFlags.Object && declaredType !== type
|
||||
|| type.flags & TypeFlags.Object && declaredType !== type && !(declaredType === unknownType && isEmptyAnonymousObjectType(type))
|
||||
|| isThisTypeParameter(type)
|
||||
|| type.flags & TypeFlags.Intersection && every((type as IntersectionType).types, t => t.symbol !== globalThisSymbol)) {
|
||||
return filterType(type, t => isTypePresencePossible(t, name, assumeTrue));
|
||||
|
||||
36
tests/baselines/reference/inKeywordAndUnknown.js
Normal file
36
tests/baselines/reference/inKeywordAndUnknown.js
Normal file
@ -0,0 +1,36 @@
|
||||
//// [inKeywordAndUnknown.ts]
|
||||
// Repro from #50531
|
||||
|
||||
function f(x: {}, y: unknown) {
|
||||
if (!("a" in x)) {
|
||||
return;
|
||||
}
|
||||
x; // {}
|
||||
if (!y) {
|
||||
return;
|
||||
}
|
||||
y; // {}
|
||||
if (!("a" in y)) {
|
||||
return;
|
||||
}
|
||||
y; // {}
|
||||
}
|
||||
|
||||
|
||||
//// [inKeywordAndUnknown.js]
|
||||
"use strict";
|
||||
// Repro from #50531
|
||||
function f(x, y) {
|
||||
if (!("a" in x)) {
|
||||
return;
|
||||
}
|
||||
x; // {}
|
||||
if (!y) {
|
||||
return;
|
||||
}
|
||||
y; // {}
|
||||
if (!("a" in y)) {
|
||||
return;
|
||||
}
|
||||
y; // {}
|
||||
}
|
||||
33
tests/baselines/reference/inKeywordAndUnknown.symbols
Normal file
33
tests/baselines/reference/inKeywordAndUnknown.symbols
Normal file
@ -0,0 +1,33 @@
|
||||
=== tests/cases/compiler/inKeywordAndUnknown.ts ===
|
||||
// Repro from #50531
|
||||
|
||||
function f(x: {}, y: unknown) {
|
||||
>f : Symbol(f, Decl(inKeywordAndUnknown.ts, 0, 0))
|
||||
>x : Symbol(x, Decl(inKeywordAndUnknown.ts, 2, 11))
|
||||
>y : Symbol(y, Decl(inKeywordAndUnknown.ts, 2, 17))
|
||||
|
||||
if (!("a" in x)) {
|
||||
>x : Symbol(x, Decl(inKeywordAndUnknown.ts, 2, 11))
|
||||
|
||||
return;
|
||||
}
|
||||
x; // {}
|
||||
>x : Symbol(x, Decl(inKeywordAndUnknown.ts, 2, 11))
|
||||
|
||||
if (!y) {
|
||||
>y : Symbol(y, Decl(inKeywordAndUnknown.ts, 2, 17))
|
||||
|
||||
return;
|
||||
}
|
||||
y; // {}
|
||||
>y : Symbol(y, Decl(inKeywordAndUnknown.ts, 2, 17))
|
||||
|
||||
if (!("a" in y)) {
|
||||
>y : Symbol(y, Decl(inKeywordAndUnknown.ts, 2, 17))
|
||||
|
||||
return;
|
||||
}
|
||||
y; // {}
|
||||
>y : Symbol(y, Decl(inKeywordAndUnknown.ts, 2, 17))
|
||||
}
|
||||
|
||||
42
tests/baselines/reference/inKeywordAndUnknown.types
Normal file
42
tests/baselines/reference/inKeywordAndUnknown.types
Normal file
@ -0,0 +1,42 @@
|
||||
=== tests/cases/compiler/inKeywordAndUnknown.ts ===
|
||||
// Repro from #50531
|
||||
|
||||
function f(x: {}, y: unknown) {
|
||||
>f : (x: {}, y: unknown) => void
|
||||
>x : {}
|
||||
>y : unknown
|
||||
|
||||
if (!("a" in x)) {
|
||||
>!("a" in x) : boolean
|
||||
>("a" in x) : boolean
|
||||
>"a" in x : boolean
|
||||
>"a" : "a"
|
||||
>x : {}
|
||||
|
||||
return;
|
||||
}
|
||||
x; // {}
|
||||
>x : {}
|
||||
|
||||
if (!y) {
|
||||
>!y : boolean
|
||||
>y : unknown
|
||||
|
||||
return;
|
||||
}
|
||||
y; // {}
|
||||
>y : {}
|
||||
|
||||
if (!("a" in y)) {
|
||||
>!("a" in y) : boolean
|
||||
>("a" in y) : boolean
|
||||
>"a" in y : boolean
|
||||
>"a" : "a"
|
||||
>y : {}
|
||||
|
||||
return;
|
||||
}
|
||||
y; // {}
|
||||
>y : {}
|
||||
}
|
||||
|
||||
18
tests/cases/compiler/inKeywordAndUnknown.ts
Normal file
18
tests/cases/compiler/inKeywordAndUnknown.ts
Normal file
@ -0,0 +1,18 @@
|
||||
// @strict: true
|
||||
|
||||
// Repro from #50531
|
||||
|
||||
function f(x: {}, y: unknown) {
|
||||
if (!("a" in x)) {
|
||||
return;
|
||||
}
|
||||
x; // {}
|
||||
if (!y) {
|
||||
return;
|
||||
}
|
||||
y; // {}
|
||||
if (!("a" in y)) {
|
||||
return;
|
||||
}
|
||||
y; // {}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user