From 3e142f8e524233b9cd6aa84f17ed0f4d3d719fdb Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Mon, 6 Feb 2017 08:47:11 -0800 Subject: [PATCH] Object literal freshness errors with spreads Previously, object literals with spreads in them would not issue object literal freshness errors. Fixes #13878 --- src/compiler/checker.ts | 2 ++ .../objectLiteralFreshnessWithSpread.errors.txt | 11 +++++++++++ .../objectLiteralFreshnessWithSpread.js | 16 ++++++++++++++++ .../compiler/objectLiteralFreshnessWithSpread.ts | 2 ++ 4 files changed, 31 insertions(+) create mode 100644 tests/baselines/reference/objectLiteralFreshnessWithSpread.errors.txt create mode 100644 tests/baselines/reference/objectLiteralFreshnessWithSpread.js create mode 100644 tests/cases/compiler/objectLiteralFreshnessWithSpread.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b5306771dd..80a2f2f6e7 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11864,6 +11864,8 @@ namespace ts { if (spread.flags & TypeFlags.Object) { // only set the symbol and flags if this is a (fresh) object type spread.flags |= propagatedFlags; + spread.flags |= TypeFlags.FreshLiteral; + (spread as ObjectType).objectFlags |= ObjectFlags.ObjectLiteral; spread.symbol = node.symbol; } return spread; diff --git a/tests/baselines/reference/objectLiteralFreshnessWithSpread.errors.txt b/tests/baselines/reference/objectLiteralFreshnessWithSpread.errors.txt new file mode 100644 index 0000000000..ac6b56f6ce --- /dev/null +++ b/tests/baselines/reference/objectLiteralFreshnessWithSpread.errors.txt @@ -0,0 +1,11 @@ +tests/cases/compiler/objectLiteralFreshnessWithSpread.ts(2,35): error TS2322: Type '{ z: number; b: number; extra: number; a: number; }' is not assignable to type '{ a: any; b: any; }'. + Object literal may only specify known properties, and 'z' does not exist in type '{ a: any; b: any; }'. + + +==== tests/cases/compiler/objectLiteralFreshnessWithSpread.ts (1 errors) ==== + let x = { b: 1, extra: 2 } + let xx: { a, b } = { a: 1, ...x, z: 3 } // error for 'z', no error for 'extra' + ~~~~ +!!! error TS2322: Type '{ z: number; b: number; extra: number; a: number; }' is not assignable to type '{ a: any; b: any; }'. +!!! error TS2322: Object literal may only specify known properties, and 'z' does not exist in type '{ a: any; b: any; }'. + \ No newline at end of file diff --git a/tests/baselines/reference/objectLiteralFreshnessWithSpread.js b/tests/baselines/reference/objectLiteralFreshnessWithSpread.js new file mode 100644 index 0000000000..0a4222603a --- /dev/null +++ b/tests/baselines/reference/objectLiteralFreshnessWithSpread.js @@ -0,0 +1,16 @@ +//// [objectLiteralFreshnessWithSpread.ts] +let x = { b: 1, extra: 2 } +let xx: { a, b } = { a: 1, ...x, z: 3 } // error for 'z', no error for 'extra' + + +//// [objectLiteralFreshnessWithSpread.js] +var __assign = (this && this.__assign) || Object.assign || function(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) + t[p] = s[p]; + } + return t; +}; +var x = { b: 1, extra: 2 }; +var xx = __assign({ a: 1 }, x, { z: 3 }); // error for 'z', no error for 'extra' diff --git a/tests/cases/compiler/objectLiteralFreshnessWithSpread.ts b/tests/cases/compiler/objectLiteralFreshnessWithSpread.ts new file mode 100644 index 0000000000..7e8044e064 --- /dev/null +++ b/tests/cases/compiler/objectLiteralFreshnessWithSpread.ts @@ -0,0 +1,2 @@ +let x = { b: 1, extra: 2 } +let xx: { a, b } = { a: 1, ...x, z: 3 } // error for 'z', no error for 'extra'