From 0600a27dd93a4759bcb062647a249d41aaadda52 Mon Sep 17 00:00:00 2001 From: Herrington Darkholme Date: Wed, 7 Jun 2017 01:08:33 +0800 Subject: [PATCH] fix #15447: `object` is empty object type (#16290) --- src/compiler/checker.ts | 1 + .../nonPrimitiveUnionIntersection.errors.txt | 18 ++++++++++++++++-- .../reference/nonPrimitiveUnionIntersection.js | 12 ++++++++++++ .../nonPrimitiveUnionIntersection.ts | 4 ++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f1aca672c52..10247343525 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8599,6 +8599,7 @@ namespace ts { function isEmptyObjectType(type: Type): boolean { return type.flags & TypeFlags.Object ? isEmptyResolvedType(resolveStructuredTypeMembers(type)) : + type.flags & TypeFlags.NonPrimitive ? true : type.flags & TypeFlags.Union ? forEach((type).types, isEmptyObjectType) : type.flags & TypeFlags.Intersection ? !forEach((type).types, t => !isEmptyObjectType(t)) : false; diff --git a/tests/baselines/reference/nonPrimitiveUnionIntersection.errors.txt b/tests/baselines/reference/nonPrimitiveUnionIntersection.errors.txt index 5fb1e89c2e3..5aad982dc20 100644 --- a/tests/baselines/reference/nonPrimitiveUnionIntersection.errors.txt +++ b/tests/baselines/reference/nonPrimitiveUnionIntersection.errors.txt @@ -1,18 +1,32 @@ tests/cases/conformance/types/nonPrimitive/nonPrimitiveUnionIntersection.ts(1,5): error TS2322: Type '""' is not assignable to type 'object & string'. Type '""' is not assignable to type 'object'. -tests/cases/conformance/types/nonPrimitive/nonPrimitiveUnionIntersection.ts(3,1): error TS2322: Type 'string' is not assignable to type 'object & string'. +tests/cases/conformance/types/nonPrimitive/nonPrimitiveUnionIntersection.ts(3,5): error TS2322: Type '123' is not assignable to type 'object & {}'. + Type '123' is not assignable to type 'object'. +tests/cases/conformance/types/nonPrimitive/nonPrimitiveUnionIntersection.ts(4,1): error TS2322: Type 'string' is not assignable to type 'object & string'. Type 'string' is not assignable to type 'object'. +tests/cases/conformance/types/nonPrimitive/nonPrimitiveUnionIntersection.ts(8,38): error TS2322: Type '{ bar: string; }' is not assignable to type 'object & { err: string; }'. + Object literal may only specify known properties, and 'bar' does not exist in type 'object & { err: string; }'. -==== tests/cases/conformance/types/nonPrimitive/nonPrimitiveUnionIntersection.ts (2 errors) ==== +==== tests/cases/conformance/types/nonPrimitive/nonPrimitiveUnionIntersection.ts (4 errors) ==== var a: object & string = ""; // error ~ !!! error TS2322: Type '""' is not assignable to type 'object & string'. !!! error TS2322: Type '""' is not assignable to type 'object'. var b: object | string = ""; // ok + var c: object & {} = 123; // error + ~ +!!! error TS2322: Type '123' is not assignable to type 'object & {}'. +!!! error TS2322: Type '123' is not assignable to type 'object'. a = b; // error ~ !!! error TS2322: Type 'string' is not assignable to type 'object & string'. !!! error TS2322: Type 'string' is not assignable to type 'object'. b = a; // ok + + const foo: object & {} = {bar: 'bar'}; // ok + const bar: object & {err: string} = {bar: 'bar'}; // error + ~~~~~~~~~~ +!!! error TS2322: Type '{ bar: string; }' is not assignable to type 'object & { err: string; }'. +!!! error TS2322: Object literal may only specify known properties, and 'bar' does not exist in type 'object & { err: string; }'. \ No newline at end of file diff --git a/tests/baselines/reference/nonPrimitiveUnionIntersection.js b/tests/baselines/reference/nonPrimitiveUnionIntersection.js index 7f4b46c42ed..771024f15e6 100644 --- a/tests/baselines/reference/nonPrimitiveUnionIntersection.js +++ b/tests/baselines/reference/nonPrimitiveUnionIntersection.js @@ -1,17 +1,29 @@ //// [nonPrimitiveUnionIntersection.ts] var a: object & string = ""; // error var b: object | string = ""; // ok +var c: object & {} = 123; // error a = b; // error b = a; // ok + +const foo: object & {} = {bar: 'bar'}; // ok +const bar: object & {err: string} = {bar: 'bar'}; // error //// [nonPrimitiveUnionIntersection.js] var a = ""; // error var b = ""; // ok +var c = 123; // error a = b; // error b = a; // ok +var foo = { bar: 'bar' }; // ok +var bar = { bar: 'bar' }; // error //// [nonPrimitiveUnionIntersection.d.ts] declare var a: object & string; declare var b: object | string; +declare var c: object & {}; +declare const foo: object & {}; +declare const bar: object & { + err: string; +}; diff --git a/tests/cases/conformance/types/nonPrimitive/nonPrimitiveUnionIntersection.ts b/tests/cases/conformance/types/nonPrimitive/nonPrimitiveUnionIntersection.ts index a9d5872705c..55c1acb03b3 100644 --- a/tests/cases/conformance/types/nonPrimitive/nonPrimitiveUnionIntersection.ts +++ b/tests/cases/conformance/types/nonPrimitive/nonPrimitiveUnionIntersection.ts @@ -1,5 +1,9 @@ // @declaration: true var a: object & string = ""; // error var b: object | string = ""; // ok +var c: object & {} = 123; // error a = b; // error b = a; // ok + +const foo: object & {} = {bar: 'bar'}; // ok +const bar: object & {err: string} = {bar: 'bar'}; // error