Skip to content

accessor allows this parameter but is not checked or enforced. #36883

@tadhgmister

Description

@tadhgmister

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:

  1. error to declare a this parameter on an accessor field (get or set)
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issueGood First IssueWell scoped, documented and has the green lightHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions