Skip to content

Commit 2e43c86

Browse files
authored
Merge pull request #15257 from Microsoft/unknown-properties-are-assignable-to-Object-in-union
Unknown properties are assignable to `Object` in union
2 parents 6de4693 + 8ab2221 commit 2e43c86

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8708,7 +8708,7 @@ namespace ts {
87088708
if (maybeTypeOfKind(target, TypeFlags.Object) && !(getObjectFlags(target) & ObjectFlags.ObjectLiteralPatternWithComputedProperties)) {
87098709
const isComparingJsxAttributes = !!(source.flags & TypeFlags.JsxAttributes);
87108710
if ((relation === assignableRelation || relation === comparableRelation) &&
8711-
(target === globalObjectType || (!isComparingJsxAttributes && isEmptyObjectType(target)))) {
8711+
(isTypeSubsetOf(globalObjectType, target) || (!isComparingJsxAttributes && isEmptyObjectType(target)))) {
87128712
return false;
87138713
}
87148714
for (const prop of getPropertiesOfObjectType(source)) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [unknownPropertiesAreAssignableToObjectUnion.ts]
2+
const x: Object | string = { x: 0 };
3+
const y: Object | undefined = { x: 0 };
4+
5+
6+
//// [unknownPropertiesAreAssignableToObjectUnion.js]
7+
var x = { x: 0 };
8+
var y = { x: 0 };
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/unknownPropertiesAreAssignableToObjectUnion.ts ===
2+
const x: Object | string = { x: 0 };
3+
>x : Symbol(x, Decl(unknownPropertiesAreAssignableToObjectUnion.ts, 0, 5))
4+
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
5+
>x : Symbol(x, Decl(unknownPropertiesAreAssignableToObjectUnion.ts, 0, 28))
6+
7+
const y: Object | undefined = { x: 0 };
8+
>y : Symbol(y, Decl(unknownPropertiesAreAssignableToObjectUnion.ts, 1, 5))
9+
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
10+
>x : Symbol(x, Decl(unknownPropertiesAreAssignableToObjectUnion.ts, 1, 31))
11+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/unknownPropertiesAreAssignableToObjectUnion.ts ===
2+
const x: Object | string = { x: 0 };
3+
>x : string | Object
4+
>Object : Object
5+
>{ x: 0 } : { x: number; }
6+
>x : number
7+
>0 : 0
8+
9+
const y: Object | undefined = { x: 0 };
10+
>y : Object | undefined
11+
>Object : Object
12+
>{ x: 0 } : { x: number; }
13+
>x : number
14+
>0 : 0
15+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// @strictNullChecks: true
2+
const x: Object | string = { x: 0 };
3+
const y: Object | undefined = { x: 0 };

0 commit comments

Comments
 (0)