Skip to content

Function returning spread operator infers wrong type and get properties can be accessedΒ #44543

Closed
@michaelcheers

Description

@michaelcheers

Bug Report

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

class Test{get Length(){return 1;}}

function spread<T> (t: T) { return {... t} }
let test1 = {...new Test()};
console.log(test1.Length); // Expected: throws error. Actual: throws error.
let test2 = (o => ({...o}))(new Test());
console.log(test2.Length); // Expected: throws error. Actual: throws error.
let test3 = spread(new Test());
console.log(test3.Length); // Expected: throws error. Actual: doesn't throw error.

πŸ™ Actual behavior

Doesn't throw compile-time error.
Error is at runtime, the spread operator of Test does not copy over properties.

πŸ™‚ Expected behavior

Typescript's type system should figure out that the type is not T but instead is just enumerable properties of T.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions