From 6d8950e13c6b586e1b55bef037e8c35d2a948cd0 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 20 Oct 2015 12:39:43 -0700 Subject: [PATCH 1/2] Fix check for excess properties in union and intersection types --- src/compiler/checker.ts | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 04cc9a2e426..823e01129d6 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4946,34 +4946,34 @@ namespace ts { resolved.stringIndexType || resolved.numberIndexType || getPropertyOfType(type, name)) { return true; } - return false; } - if (type.flags & TypeFlags.UnionOrIntersection) { + else if (type.flags & TypeFlags.UnionOrIntersection) { for (let t of (type).types) { if (isKnownProperty(t, name)) { return true; } } - return false; } - return true; + return false; } function hasExcessProperties(source: FreshObjectLiteralType, target: Type, reportErrors: boolean): boolean { - for (let prop of getPropertiesOfObjectType(source)) { - if (!isKnownProperty(target, prop.name)) { - if (reportErrors) { - // We know *exactly* where things went wrong when comparing the types. - // Use this property as the error node as this will be more helpful in - // reasoning about what went wrong. - errorNode = prop.valueDeclaration; - reportError(Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1, - symbolToString(prop), - typeToString(target)); + if (someConstituentTypeHasKind(target, TypeFlags.ObjectType)) { + for (let prop of getPropertiesOfObjectType(source)) { + if (!isKnownProperty(target, prop.name)) { + if (reportErrors) { + // We know *exactly* where things went wrong when comparing the types. + // Use this property as the error node as this will be more helpful in + // reasoning about what went wrong. + errorNode = prop.valueDeclaration; + reportError(Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1, + symbolToString(prop), typeToString(target)); + } + return true; } - return true; } } + return false; } function eachTypeRelatedToSomeType(source: UnionOrIntersectionType, target: UnionOrIntersectionType): Ternary { From a22f9b87bd979872f4ce0fe75759f184c15930c4 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 20 Oct 2015 12:40:39 -0700 Subject: [PATCH 2/2] Adding test --- .../objectLiteralExcessProperties.errors.txt | 66 +++++++++++++++++++ .../objectLiteralExcessProperties.js | 32 +++++++++ .../compiler/objectLiteralExcessProperties.ts | 21 ++++++ 3 files changed, 119 insertions(+) create mode 100644 tests/baselines/reference/objectLiteralExcessProperties.errors.txt create mode 100644 tests/baselines/reference/objectLiteralExcessProperties.js create mode 100644 tests/cases/compiler/objectLiteralExcessProperties.ts diff --git a/tests/baselines/reference/objectLiteralExcessProperties.errors.txt b/tests/baselines/reference/objectLiteralExcessProperties.errors.txt new file mode 100644 index 00000000000..84f635bf947 --- /dev/null +++ b/tests/baselines/reference/objectLiteralExcessProperties.errors.txt @@ -0,0 +1,66 @@ +tests/cases/compiler/objectLiteralExcessProperties.ts(9,18): error TS2322: Type '{ forword: string; }' is not assignable to type 'Book'. + Object literal may only specify known properties, and 'forword' does not exist in type 'Book'. +tests/cases/compiler/objectLiteralExcessProperties.ts(11,27): error TS2322: Type '{ foreward: string; }' is not assignable to type 'Book | string'. + Object literal may only specify known properties, and 'foreward' does not exist in type 'Book | string'. +tests/cases/compiler/objectLiteralExcessProperties.ts(13,53): error TS2322: Type '({ foreword: string; } | { forwards: string; })[]' is not assignable to type 'Book | Book[]'. + Type '({ foreword: string; } | { forwards: string; })[]' is not assignable to type 'Book[]'. + Type '{ foreword: string; } | { forwards: string; }' is not assignable to type 'Book'. + Type '{ forwards: string; }' is not assignable to type 'Book'. + Object literal may only specify known properties, and 'forwards' does not exist in type 'Book'. +tests/cases/compiler/objectLiteralExcessProperties.ts(15,42): error TS2322: Type '{ foreword: string; colour: string; }' is not assignable to type 'Book & Cover'. + Object literal may only specify known properties, and 'colour' does not exist in type 'Book & Cover'. +tests/cases/compiler/objectLiteralExcessProperties.ts(17,26): error TS2322: Type '{ foreward: string; color: string; }' is not assignable to type 'Book & Cover'. + Object literal may only specify known properties, and 'foreward' does not exist in type 'Book & Cover'. +tests/cases/compiler/objectLiteralExcessProperties.ts(19,57): error TS2322: Type '{ foreword: string; color: string; price: number; }' is not assignable to type 'Book & Cover'. + Object literal may only specify known properties, and 'price' does not exist in type 'Book & Cover'. +tests/cases/compiler/objectLiteralExcessProperties.ts(21,43): error TS2322: Type '{ foreword: string; price: number; }' is not assignable to type 'Book & number'. + Object literal may only specify known properties, and 'price' does not exist in type 'Book & number'. + + +==== tests/cases/compiler/objectLiteralExcessProperties.ts (7 errors) ==== + interface Book { + foreword: string; + } + + interface Cover { + color?: string; + } + + var b1: Book = { forword: "oops" }; + ~~~~~~~~~~~~~~~ +!!! error TS2322: Type '{ forword: string; }' is not assignable to type 'Book'. +!!! error TS2322: Object literal may only specify known properties, and 'forword' does not exist in type 'Book'. + + var b2: Book | string = { foreward: "nope" }; + ~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '{ foreward: string; }' is not assignable to type 'Book | string'. +!!! error TS2322: Object literal may only specify known properties, and 'foreward' does not exist in type 'Book | string'. + + var b3: Book | (Book[]) = [{ foreword: "hello" }, { forwards: "back" }]; + ~~~~~~~~~~~~~~~~ +!!! error TS2322: Type '({ foreword: string; } | { forwards: string; })[]' is not assignable to type 'Book | Book[]'. +!!! error TS2322: Type '({ foreword: string; } | { forwards: string; })[]' is not assignable to type 'Book[]'. +!!! error TS2322: Type '{ foreword: string; } | { forwards: string; }' is not assignable to type 'Book'. +!!! error TS2322: Type '{ forwards: string; }' is not assignable to type 'Book'. +!!! error TS2322: Object literal may only specify known properties, and 'forwards' does not exist in type 'Book'. + + var b4: Book & Cover = { foreword: "hi", colour: "blue" }; + ~~~~~~~~~~~~~~ +!!! error TS2322: Type '{ foreword: string; colour: string; }' is not assignable to type 'Book & Cover'. +!!! error TS2322: Object literal may only specify known properties, and 'colour' does not exist in type 'Book & Cover'. + + var b5: Book & Cover = { foreward: "hi", color: "blue" }; + ~~~~~~~~~~~~~~ +!!! error TS2322: Type '{ foreward: string; color: string; }' is not assignable to type 'Book & Cover'. +!!! error TS2322: Object literal may only specify known properties, and 'foreward' does not exist in type 'Book & Cover'. + + var b6: Book & Cover = { foreword: "hi", color: "blue", price: 10.99 }; + ~~~~~~~~~~~~ +!!! error TS2322: Type '{ foreword: string; color: string; price: number; }' is not assignable to type 'Book & Cover'. +!!! error TS2322: Object literal may only specify known properties, and 'price' does not exist in type 'Book & Cover'. + + var b7: Book & number = { foreword: "hi", price: 10.99 }; + ~~~~~~~~~~~~ +!!! error TS2322: Type '{ foreword: string; price: number; }' is not assignable to type 'Book & number'. +!!! error TS2322: Object literal may only specify known properties, and 'price' does not exist in type 'Book & number'. + \ No newline at end of file diff --git a/tests/baselines/reference/objectLiteralExcessProperties.js b/tests/baselines/reference/objectLiteralExcessProperties.js new file mode 100644 index 00000000000..747928ccb20 --- /dev/null +++ b/tests/baselines/reference/objectLiteralExcessProperties.js @@ -0,0 +1,32 @@ +//// [objectLiteralExcessProperties.ts] +interface Book { + foreword: string; +} + +interface Cover { + color?: string; +} + +var b1: Book = { forword: "oops" }; + +var b2: Book | string = { foreward: "nope" }; + +var b3: Book | (Book[]) = [{ foreword: "hello" }, { forwards: "back" }]; + +var b4: Book & Cover = { foreword: "hi", colour: "blue" }; + +var b5: Book & Cover = { foreward: "hi", color: "blue" }; + +var b6: Book & Cover = { foreword: "hi", color: "blue", price: 10.99 }; + +var b7: Book & number = { foreword: "hi", price: 10.99 }; + + +//// [objectLiteralExcessProperties.js] +var b1 = { forword: "oops" }; +var b2 = { foreward: "nope" }; +var b3 = [{ foreword: "hello" }, { forwards: "back" }]; +var b4 = { foreword: "hi", colour: "blue" }; +var b5 = { foreward: "hi", color: "blue" }; +var b6 = { foreword: "hi", color: "blue", price: 10.99 }; +var b7 = { foreword: "hi", price: 10.99 }; diff --git a/tests/cases/compiler/objectLiteralExcessProperties.ts b/tests/cases/compiler/objectLiteralExcessProperties.ts new file mode 100644 index 00000000000..0aa81703c33 --- /dev/null +++ b/tests/cases/compiler/objectLiteralExcessProperties.ts @@ -0,0 +1,21 @@ +interface Book { + foreword: string; +} + +interface Cover { + color?: string; +} + +var b1: Book = { forword: "oops" }; + +var b2: Book | string = { foreward: "nope" }; + +var b3: Book | (Book[]) = [{ foreword: "hello" }, { forwards: "back" }]; + +var b4: Book & Cover = { foreword: "hi", colour: "blue" }; + +var b5: Book & Cover = { foreward: "hi", color: "blue" }; + +var b6: Book & Cover = { foreword: "hi", color: "blue", price: 10.99 }; + +var b7: Book & number = { foreword: "hi", price: 10.99 };