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 calling x.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.
TypeScript Version: 3.7.5 and 3.8 (91ffa1c)
Search Terms:
getter, get, set, setter, accessor property, this parameter
Code
In the following,
x.aandx.b()are invalid for the same reason butx.adoesn't give a type error.Expected behavior is one of:
thisparameter on an accessor field (get or set)x.agives same type error as callingx.b()Actual behavior:
thisparameter is syntactically allowed on accessor but has no impact outside the function so it is never checked. Accessingx.athrows 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.