Skip to content

Incorrect type inference of object spread, when spreaded object is referenced via interface and created by class instantiation #13409

Closed
@OleksandrNechai

Description

@OleksandrNechai

TypeScript Version: 2.1.X (Playground)

Code

interface I { x: number, y: number, z: () => void }

class MyClass implements I {
  constructor(public x: number, public y: number) {
  }
  z() { alert(this.x + this.y) }
}

var x: I = new MyClass(5, 7);
var o = { ...x }
alert(o.z); // undefined

Expected behavior:
Compile error. Either MyClass incorrectly implements I interface requiring to define z as z=()=>alert(this.x + this.y) or at alert(o.z) reporting there is no such a property on o, or report something similar to FlowType.

Actual behavior:
No compiler warnings and runtime error when trying to invoke o.z().

Note:
When I change a definition of I this way: interface I { x: number, y: number, z(): void }, I get an error Property 'z' does not exist on type '{ x: number; y: number; }' as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions