Skip to content

Commit 86661b5

Browse files
authored
Merge pull request #13900 from Microsoft/object-literal-freshness-with-spread
Object literal freshness errors with spreads
2 parents fc306ba + 3e142f8 commit 86661b5

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

src/compiler/checker.ts

+2
Original file line numberDiff line numberDiff line change
@@ -13306,6 +13306,8 @@ namespace ts {
1330613306
if (spread.flags & TypeFlags.Object) {
1330713307
// only set the symbol and flags if this is a (fresh) object type
1330813308
spread.flags |= propagatedFlags;
13309+
spread.flags |= TypeFlags.FreshLiteral;
13310+
(spread as ObjectType).objectFlags |= ObjectFlags.ObjectLiteral;
1330913311
spread.symbol = node.symbol;
1331013312
}
1331113313
return spread;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
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; }'.
2+
Object literal may only specify known properties, and 'z' does not exist in type '{ a: any; b: any; }'.
3+
4+
5+
==== tests/cases/compiler/objectLiteralFreshnessWithSpread.ts (1 errors) ====
6+
let x = { b: 1, extra: 2 }
7+
let xx: { a, b } = { a: 1, ...x, z: 3 } // error for 'z', no error for 'extra'
8+
~~~~
9+
!!! error TS2322: Type '{ z: number; b: number; extra: number; a: number; }' is not assignable to type '{ a: any; b: any; }'.
10+
!!! error TS2322: Object literal may only specify known properties, and 'z' does not exist in type '{ a: any; b: any; }'.
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [objectLiteralFreshnessWithSpread.ts]
2+
let x = { b: 1, extra: 2 }
3+
let xx: { a, b } = { a: 1, ...x, z: 3 } // error for 'z', no error for 'extra'
4+
5+
6+
//// [objectLiteralFreshnessWithSpread.js]
7+
var __assign = (this && this.__assign) || Object.assign || function(t) {
8+
for (var s, i = 1, n = arguments.length; i < n; i++) {
9+
s = arguments[i];
10+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
11+
t[p] = s[p];
12+
}
13+
return t;
14+
};
15+
var x = { b: 1, extra: 2 };
16+
var xx = __assign({ a: 1 }, x, { z: 3 }); // error for 'z', no error for 'extra'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let x = { b: 1, extra: 2 }
2+
let xx: { a, b } = { a: 1, ...x, z: 3 } // error for 'z', no error for 'extra'

0 commit comments

Comments
 (0)