microsoft/TypeScript

Maintain freshness on the result of onbject spread operator

mhegazy opened this issue ยท 5 comments

interface I { 
    a: number;
    b: string;
}

var obj = { c: true };

var i: I = { a: 0, b: "s", c: obj.c };// Error, unknown property c
var i: I = { a: 0, b: "s", ...obj } // No error here.

As noted in #2103 (comment), spreading a symbolic name is different from adding the property definition in the object literal. in the later, an excess property is likely an error; in the earlier it is not necessarily one.

@mhegazy

Why was this closed? Maybe I didn't understand but this is still an issue:

interface Foo {
  a: number;
  b: string;
}

declare const stuff: any;

const a: Foo = { a: 1, b: "b" } // Ok
const b: Foo = { a: 1, b: "b", c: "c" } // Error - Ok
const c: Foo = { a: 1, b: "b", c: "c", ...stuff } // No Error - not Ok!

I don't understand why the presence of ...stuff makes the unknown typed property c become allowed. If it was part of stuff I understand this is a "bag or properties" you don't want to error on unknown properties, but why does it allow previous errors to become allowed?

I'm facing the same issue why was that declined?

An update on this issue. we have revisited the original decision (more in #14853), and some of the patterns involving spread should be flagged now as errors in TS 2.4, see #15908.

@mhegazy That's great news! Thanks for the update!