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 method assigned to a variable has incorrect inferred type #50113

Closed
olehmisar opened this issue Jul 31, 2022 · 2 comments
Closed

Class method assigned to a variable has incorrect inferred type #50113

olehmisar opened this issue Jul 31, 2022 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@olehmisar
Copy link

Bug Report

πŸ”Ž Search Terms

class method function assignment variable this bind

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

class User {
    name: string | undefined
    setName(name: string) {
        this.name = name
    }
}

const user = new User()
const setNameFunction = user.setName
setNameFunction('bob')

πŸ™ Actual behavior

  • the type of setNameFunction is inferred to be (name: string) => void
  • code compiled successfully
  • runtime execution failed: Cannot set properties of undefined (setting 'name')

πŸ™‚ Expected behavior

  • the type of setNameFunction should be inferred to be (this: User, name: string) => void
  • code should not compile because setNameFunction('bob') will fail in runtime
@whzx5byb
Copy link

See #7968 and #6739

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 2, 2022
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for 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

4 participants