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

ThisType doesn't enforce type-checking Object.prototype.call #59341

Closed
mkermani144 opened this issue Jul 18, 2024 · 5 comments
Closed

ThisType doesn't enforce type-checking Object.prototype.call #59341

mkermani144 opened this issue Jul 18, 2024 · 5 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@mkermani144
Copy link

mkermani144 commented Jul 18, 2024

πŸ”Ž Search Terms

  • ThisType
  • ThisType and call

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about ThisType

⏯ Playground Link

https://www.typescriptlang.org/play/?ssl=8&ssc=40&pln=1&pc=1#code/MYewdgzgLgBAYiEAuGAVAFgSwqgngBwFMAeAbxgA8UwBXAWwCNCAnGAXwD4YAyGcgM0QAKAJTV6TVmxgBePgCgYMQSFEKlSgPSaYoSCAA2hAHQGQAcyFQsEY7hEBuGNpjEAtG5gtmIZoo3MhFA0zGAw1tjGFA7+bPJx8gggxirGwACGBgZC5LgoAEzsjs467p5gIF7MPsxAA

πŸ’» Code

const Foo: ThisType<{ x: number }> & { foo(): number } = {
  foo() {
    // console.log(this.y); // <-- error
    return this.x;
  }
}

Foo.foo.call({ y: 2 }); // <-- no error

πŸ™ Actual behavior

The last line doesn't give an error.

πŸ™‚ Expected behavior

The last line should give an error.

Additional information about the issue

No response

@jcalz
Copy link
Contributor

jcalz commented Jul 18, 2024

ThisType doesn't do what you think it does; it just gives a contextual type to object literals, it doesn't actually constrain it from the outside. You're probably looking for a this parameter instead:

const Foo: { foo(this: { x: number }): number } = {
  foo() {
    console.log(this.y); // <-- error
    return this.x; // okay
  }
}

Foo.foo.call({ y: 2 }); // <-- error

Playground link

@mkermani144
Copy link
Author

Thanks for the quick reply @jcalz.

I know about this parameters, but my point here is that these features may conflict. In addition, when I use ThisType, I don't want to duplicate the this type for each of the functions in an object.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Jul 26, 2024
@RyanCavanaugh
Copy link
Member

It's not a bug that ThisType doesn't do two different things at once

@mkermani144
Copy link
Author

It's not a bug that ThisType doesn't do two different things at once

I understand. I'm not necessarily stating it's a bug. Maybe it's more like a new feature. By the way, I think it may benefit developers. Or maybe I'm wrong.

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants