-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issueGood First IssueWell scoped, documented and has the green lightWell scoped, documented and has the green lightHelp WantedYou can do thisYou can do this
Milestone
Description
TypeScript Version: 3.7.5 and 3.8 (91ffa1c)
Search Terms:
getter, get, set, setter, accessor property, this parameter
Code
In the following, x.a
and x.b()
are invalid for the same reason but x.a
doesn't give a type error.
interface Unimplemented {
calculate(): number;
}
class Demo {
get a(this: Unimplemented) {
return this.calculate();
}
b(this: Unimplemented) {
return this.calculate();
}
}
const x = new Demo();
console.log(x.a); // no type error, fails at runtime
console.log(x.b()) // correctly gives following error:
/* The 'this' context of type 'Demo' is not assignable to method's 'this' of type 'Unimplemented'.
Property 'calculate' is missing in type 'Demo' but required in type 'Unimplemented'.ts(2684) */
Expected behavior is one of:
- error to declare a
this
parameter on an accessor field (get or set) - OR accessing
x.a
gives same type error as callingx.b()
Actual behavior:
this
parameter is syntactically allowed on accessor but has no impact outside the function so it is never checked. Accessing x.a
throws an error at runtime.
Playground Link:
Playground Link
Related Issues:
none found, It's quite possible I'm the first one to actually try something like this.
uhyo
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFixedA PR has been merged for this issueA PR has been merged for this issueGood First IssueWell scoped, documented and has the green lightWell scoped, documented and has the green lightHelp WantedYou can do thisYou can do this