-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Flag for strict default function this
types for call-site and assignability checking (--strictThis
)
#7968
Comments
Please see #7689 for the relevant desing discussion |
I just ran into a problem where the obvious solution would be that methods without an explicit |
Maybe we can revive this thing as I believe it would bring a lot of benefit. Regarding the "backwards compatibility" problem - is this due to class-this context for methods which in reality should stay |
I also hope this is added back. I just started using Typescript last week, and this is basically the only "type error" I've run into (and repeatedly for that matter) in my all Typescript codebase. I hit it frequently, as I am using an ES6 classes as a router/controller in my Express app, e.g. I've been impressed with Typescript in general but was very surprised to hear that it's not checked at compile time since I can't think of another typed language, where such an error is possible. |
Hi, I was surprised that strict type checking for the As already mentioned in previous comments, the issues #3694 and #6018 have already proposed some changes and some of them were already implemented in the pull requests #4910 and #6739. Since there is a long history, I found it difficult to get an overview about the situation. I therefore tried to summarize it below. I hope that I did not mix up anything. At the bottom, I bring up my own thoughts about the current situation. Implemented✔️ Polymorphic |
Maybe a partial solution with some usefulness would be to check this whenever it was defined explicitly at source and only then. If the "full" solution with implicit |
Even if the correct deduction of the class Foo {
_a = 5;
f() { console.log(this._a); }
}
let foo = new Foo();
let cb: () => void = foo.f;
cb(); // oops compiles under strict1 is just very unfortunate and leads to a lot of subtle errors that could be easily avoided even without a perfect deduction of the Footnotes
|
I think I ran into this as well - https://www.typescriptlang.org/play?#code/MYGwhgzhAEAKCmAnCB7AdtA3gKGtNYAtvAFzQQAuiAlmgOYDcu0w6liArsBSogBQFiZdrToBKLMzwUAFtQgA6QfGgBefEXhM8AX2YQwATwBymvhJx48rNKhDwFIFHT6z5SzWO3Q9e+xWgAIxRAtXx4AHc4JFQ0PgByACEQ+K9sf2gONGCsgBMw4MCFAxNNJiyctFzzJiA - if you hover over the sayName on the second to last line, it says "Person.sayName()", which is wrong, because it implies it's a static method when it's actually an instance method. |
Currently TS shows tooltip of instance properties/methods vs static properties/methods exactly the same. |
I hope that this feature will someday be included into the release :) |
I ran into an issue that I'd love to avoid in the future via a flag like this: class Foo {
fighters = []
popFighter() {
return this.fighters.pop()
}
}
class Bar {
foo = new Foo()
get popFighter() {
// this ends up being the Bar instance here when called in JS
// if the user doesn't know it needs to be bound here (like I didn't)
return this.foo.popFighter
}
}
const bar = new Bar()
// runtime error: cannot read properties of undefined (reading 'pop')
bar.popFighter() //?
class FooWithThis {
fighters = []
// can also use `this: this`
popFighter(this: FooWithThis) {
return this.fighters.pop()
}
}
class Bar2 {
foo = new FooWithThis()
get popFighter() {
// this ends up being the Bar instance here when called in JS
// if the user doesn't know it needs to be bound here (like I didn't)
return this.foo.popFighter
}
}
const bar2 = new Bar2()
// TS will actually warn us here that the `this` context is wrong as long as it was explicitly typed 😍
bar2.popFighter()
// The problem is it's hard to know how a method on a class might be referenced like this when the
// class is being written. I assume the actual implementation work involved must be relatively limited since TS can already identify this problem if the type is manually specified (which I was very impressed with!)? Could probably help catch quite a few of JS's notorious this-related gotchas. |
Why would you use a getter there instead of |
@ljharb This is a repro meant to be minimal not realistic. |
Maybe it helps if I put everything I learned earlier in the form of a proposal. Note that I am not really an active TypeScript user at the moment, but I still think this is an important issue and I would like to see it fixed when I return to TypeScript eventually. It would be interesting to know if this proposal would have a chance of being accepted if someone created a Pull Request. Problem Statement (Motivation)The type of Back when class components were still a thing in React, the official React documentation also spent a large portion of Handling Events discussing this problem as part of their introduction. The option This proposal introduces a compiler flag that allows the compiler to detect these types of errors at compile time. While the runtime error was an aftereffect (which was often delayed), with this proposal the compiler can pinpoint the problematic location in the source code at compile time.
|
#6018 was originally going to add a
--strictThis
flag that made functionthis
types default tovoid
or the enclosing class (instead ofany
) for purposes of call-site and assignability checking, but that functionality was dropped (details). This is a follow-up suggestion for the dropped functionality, whatever the flag ends up being named.The text was updated successfully, but these errors were encountered: