Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class properties are not definitely assigned in constructor when using Object.assign #28883

Closed
lightclient opened this issue Dec 6, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@lightclient
Copy link

TypeScript Version: 3.1.6

Search Terms:

  • merge objects typescript
  • object.assign typescript

Code

interface Identity {
    name: string
}

class Person implements Identity {
    name: string

    constructor(id: Identity) {
        Object.assign(this, id)
    }
}

Expected behavior:
Assign the Person's name property to the name property in id.

Actual behavior:
Compilation error: Property 'name' has no initializer and is not definitely assigned in the constructor. [2564]

Playground Link: http://www.typescriptlang.org/play/#src=interface%20Identity%20%7B%0D%0A%20%20%20%20name%3A%20string%0D%0A%7D%0D%0A%0D%0Aclass%20Person%20implements%20Identity%20%7B%0D%0A%20%20%20%20name%3A%20string%0D%0A%0D%0A%20%20%20%20constructor(id%3A%20Identity)%20%7B%0D%0A%20%20%20%20%20%20%20%20Object.assign(this%2C%20id)%0D%0A%20%20%20%20%7D%0D%0A%7D

be sure to click on strictNullChecks, strictFunctionTypes, and strictPropertyInitialization

@weswigham
Copy link
Member

Duplicate of #26792

@weswigham weswigham marked this as a duplicate of #26792 Dec 6, 2018
@weswigham weswigham added the Duplicate An existing issue was already created label Dec 6, 2018
@typescript-bot
Copy link
Collaborator

This issue has been marked as a duplicate and has seen no activity in the last day. It has been closed automatic house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants