Merge pull request #15257 from Microsoft/unknown-properties-are-assignable-to-Object-in-union

Unknown properties are assignable to `Object` in union
This commit is contained in:
Nathan Shively-Sanders 2017-04-18 15:21:46 -07:00 committed by GitHub
commit 2e43c869b4
5 changed files with 38 additions and 1 deletions

View file

@ -8708,7 +8708,7 @@ namespace ts {
if (maybeTypeOfKind(target, TypeFlags.Object) && !(getObjectFlags(target) & ObjectFlags.ObjectLiteralPatternWithComputedProperties)) {
const isComparingJsxAttributes = !!(source.flags & TypeFlags.JsxAttributes);
if ((relation === assignableRelation || relation === comparableRelation) &&
(target === globalObjectType || (!isComparingJsxAttributes && isEmptyObjectType(target)))) {
(isTypeSubsetOf(globalObjectType, target) || (!isComparingJsxAttributes && isEmptyObjectType(target)))) {
return false;
}
for (const prop of getPropertiesOfObjectType(source)) {

View file

@ -0,0 +1,8 @@
//// [unknownPropertiesAreAssignableToObjectUnion.ts]
const x: Object | string = { x: 0 };
const y: Object | undefined = { x: 0 };
//// [unknownPropertiesAreAssignableToObjectUnion.js]
var x = { x: 0 };
var y = { x: 0 };

View file

@ -0,0 +1,11 @@
=== tests/cases/compiler/unknownPropertiesAreAssignableToObjectUnion.ts ===
const x: Object | string = { x: 0 };
>x : Symbol(x, Decl(unknownPropertiesAreAssignableToObjectUnion.ts, 0, 5))
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(unknownPropertiesAreAssignableToObjectUnion.ts, 0, 28))
const y: Object | undefined = { x: 0 };
>y : Symbol(y, Decl(unknownPropertiesAreAssignableToObjectUnion.ts, 1, 5))
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>x : Symbol(x, Decl(unknownPropertiesAreAssignableToObjectUnion.ts, 1, 31))

View file

@ -0,0 +1,15 @@
=== tests/cases/compiler/unknownPropertiesAreAssignableToObjectUnion.ts ===
const x: Object | string = { x: 0 };
>x : string | Object
>Object : Object
>{ x: 0 } : { x: number; }
>x : number
>0 : 0
const y: Object | undefined = { x: 0 };
>y : Object | undefined
>Object : Object
>{ x: 0 } : { x: number; }
>x : number
>0 : 0

View file

@ -0,0 +1,3 @@
// @strictNullChecks: true
const x: Object | string = { x: 0 };
const y: Object | undefined = { x: 0 };