GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — missing properties after applying spread operator on instance of abstract type · Issue #32022 · microsoft/TypeScript

missing properties after applying spread operator on instance of abstract type  #32022

New issue
Copy link
New issue
Copy link
Closed
Closed
missing properties after applying spread operator on instance of abstract type #32022
Copy link
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed

Description

@paulluap
Paul Lu (paulluap)
opened on Jun 21, 2019
Issue body actions

TypeScript Version: 3.5.2

Search Terms:
spread operator type error, Object.assign,
Code

abstract class Base { 
    abstract get a(): string;
    abstract get b(): string;
}
class A extends Base{ 
    a: string
    b: string
}
const a1 : Base = new A() 
const a2 : Base = {...a1}  //reports error: Type '{}' is missing the following properties from type 'Base': a, bts(2739)

const a3 : Base = a1
const a4 : Base = Object.assign({}, a1)

Expected behavior:
code compiles

Actual behavior:
reports error on a2
Type '{}' is missing the following properties from type 'Base': a, bts(2739)

Playground Link:
https://www.typescriptlang.org/play/#src=abstract%20class%20Base%20%7B%20%0D%0A%20%20%20%20abstract%20get%20a()%3A%20string%3B%0D%0A%20%20%20%20abstract%20get%20b()%3A%20string%3B%0D%0A%7D%0D%0Aclass%20A%20extends%20Base%7B%20%0D%0A%20%20%20%20a%3A%20string%0D%0A%20%20%20%20b%3A%20string%0D%0A%7D%0D%0Aconst%20a1%20%3A%20Base%20%3D%20new%20A()%20%0D%0Aconst%20a2%20%3A%20Base%20%3D%20%7B...a1%7D%0D%0Aconst%20a3%20%3A%20Base%20%3D%20a1%0D%0Aconst%20a4%20%3A%20Base%20%3D%20Object.assign(%7B%7D%2C%20a1)

Related Issues:

Reacted by Robouste

Activity

  1. jcalz commented on Jun 21, 2019

    @jcalz
    Joe Calzaretta (jcalz)
    on Jun 21, 2019
    Contributor
    More actions

    In classes, getters go on the prototype like methods, and so object spread won't copy them:

    class X {
      get x() {
        return "x";
      }
    }
    let y = { ...new X() }; // {}
    console.log(y); // {}

    Since a1 is only known to be Base, I think the a2 behavior is probably working as intended.

    That the a4 behavior differs is likely a design limitation; the return type of Object.assign() is an intersection of its input types, which is incorrect for some edge cases.

    This is related to #10727 and #28234 about how we don't have a real spread type and tend to use intersections in generic cases and Object.assign(), but that spread expressions with known concrete class instance only preserve non-method properties.

    Reacted by Paul Lu and Svyat Kovtun
  2. Anders Hejlsberg (ahejlsberg)
    added
    Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
    on Jun 21, 2019
  3. fatcerberus commented on Jun 22, 2019

    @fatcerberus
    Bruce Pascoe (fatcerberus)
    on Jun 22, 2019
    More actions

    Yeah, I think this is the same deal as how in C++ if you only have a Base reference and call a method on it, you get the base class implementation unless the method is virtual (which requires a vtable, i.e. runtime dispatch). TS types have no runtime representation so if the compiler sees Base then it has no way to know it’s really dealing with a Derived.

  4. paulluap commented on Jun 24, 2019

    @paulluap
    Paul Lu (paulluap)
    on Jun 24, 2019
    Author
    More actions

    I see. Thanks for the replies.

  5. Paul Lu (paulluap)
    closed this as completedon Jun 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints 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

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.