From 89fb304eeede783031f15ea298cd4289bd533977 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Wed, 25 May 2016 10:18:49 -0700 Subject: [PATCH] Add test case for excess checking of numeric properties --- .../objectLiteralExcessProperties.errors.txt | 19 ++++++++++++++++++- .../objectLiteralExcessProperties.js | 10 ++++++++++ .../compiler/objectLiteralExcessProperties.ts | 8 ++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/tests/baselines/reference/objectLiteralExcessProperties.errors.txt b/tests/baselines/reference/objectLiteralExcessProperties.errors.txt index cfae572d6d2..b96b9b0c542 100644 --- a/tests/baselines/reference/objectLiteralExcessProperties.errors.txt +++ b/tests/baselines/reference/objectLiteralExcessProperties.errors.txt @@ -19,9 +19,13 @@ tests/cases/compiler/objectLiteralExcessProperties.ts(23,29): error TS2322: Type Object literal may only specify known properties, and 'couleur' does not exist in type 'Cover | Cover[]'. tests/cases/compiler/objectLiteralExcessProperties.ts(25,27): error TS2322: Type '{ forewarned: string; }' is not assignable to type 'Book | Book[]'. Object literal may only specify known properties, and 'forewarned' does not exist in type 'Book | Book[]'. +tests/cases/compiler/objectLiteralExcessProperties.ts(33,27): error TS2322: Type '{ 0: { colour: string; }; }' is not assignable to type 'Indexed'. + Property '0' is incompatible with index signature. + Type '{ colour: string; }' is not assignable to type 'Cover'. + Object literal may only specify known properties, and 'colour' does not exist in type 'Cover'. -==== tests/cases/compiler/objectLiteralExcessProperties.ts (9 errors) ==== +==== tests/cases/compiler/objectLiteralExcessProperties.ts (10 errors) ==== interface Book { foreword: string; } @@ -77,4 +81,17 @@ tests/cases/compiler/objectLiteralExcessProperties.ts(25,27): error TS2322: Type ~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2322: Type '{ forewarned: string; }' is not assignable to type 'Book | Book[]'. !!! error TS2322: Object literal may only specify known properties, and 'forewarned' does not exist in type 'Book | Book[]'. + + interface Indexed { + [n: number]: Cover; + } + + var b10: Indexed = { 0: { }, '1': { } }; // ok + + var b11: Indexed = { 0: { colour: "blue" } }; // nested object literal still errors + ~~~~~~~~~~~~~~ +!!! error TS2322: Type '{ 0: { colour: string; }; }' is not assignable to type 'Indexed'. +!!! error TS2322: Property '0' is incompatible with index signature. +!!! error TS2322: Type '{ colour: string; }' is not assignable to type 'Cover'. +!!! error TS2322: Object literal may only specify known properties, and 'colour' does not exist in type 'Cover'. \ No newline at end of file diff --git a/tests/baselines/reference/objectLiteralExcessProperties.js b/tests/baselines/reference/objectLiteralExcessProperties.js index 3515723b5f1..38186b19bf7 100644 --- a/tests/baselines/reference/objectLiteralExcessProperties.js +++ b/tests/baselines/reference/objectLiteralExcessProperties.js @@ -24,6 +24,14 @@ var b7: Book & number = { foreword: "hi", price: 10.99 }; var b8: Cover | Cover[] = { couleur : "non" }; var b9: Book | Book[] = { forewarned: "still no" }; + +interface Indexed { + [n: number]: Cover; +} + +var b10: Indexed = { 0: { }, '1': { } }; // ok + +var b11: Indexed = { 0: { colour: "blue" } }; // nested object literal still errors //// [objectLiteralExcessProperties.js] @@ -36,3 +44,5 @@ var b6 = { foreword: "hi", color: "blue", price: 10.99 }; var b7 = { foreword: "hi", price: 10.99 }; var b8 = { couleur: "non" }; var b9 = { forewarned: "still no" }; +var b10 = { 0: {}, '1': {} }; // ok +var b11 = { 0: { colour: "blue" } }; // nested object literal still errors diff --git a/tests/cases/compiler/objectLiteralExcessProperties.ts b/tests/cases/compiler/objectLiteralExcessProperties.ts index 7ed64d2d42c..ca681b7b28e 100644 --- a/tests/cases/compiler/objectLiteralExcessProperties.ts +++ b/tests/cases/compiler/objectLiteralExcessProperties.ts @@ -23,3 +23,11 @@ var b7: Book & number = { foreword: "hi", price: 10.99 }; var b8: Cover | Cover[] = { couleur : "non" }; var b9: Book | Book[] = { forewarned: "still no" }; + +interface Indexed { + [n: number]: Cover; +} + +var b10: Indexed = { 0: { }, '1': { } }; // ok + +var b11: Indexed = { 0: { colour: "blue" } }; // nested object literal still errors