Skip to content

Commit 3e142f8

Browse files
committed
Object literal freshness errors with spreads
Previously, object literals with spreads in them would not issue object literal freshness errors. Fixes #13878
1 parent 501084a commit 3e142f8

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
@@ -11864,6 +11864,8 @@ namespace ts {
1186411864
if (spread.flags & TypeFlags.Object) {
1186511865
// only set the symbol and flags if this is a (fresh) object type
1186611866
spread.flags |= propagatedFlags;
11867+
spread.flags |= TypeFlags.FreshLiteral;
11868+
(spread as ObjectType).objectFlags |= ObjectFlags.ObjectLiteral;
1186711869
spread.symbol = node.symbol;
1186811870
}
1186911871
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)