microsoft / microsoft/TypeScript

Allow accessors to support inline generics for this

Open
#35,986 6 comments 53 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Search Terms

getter, accessor, this, generic

Suggestion

Currently accessor declarations do not support generics at the declaration site. The error returned is An accessor cannot have type parameters.ts(1094). Currently the accessor can implement generic declaration via the enclosing class. The goal would be to allow for the accessor to support a generic this at the declaration site.

Use Cases

The use case is to allow for more expressive setter/getter declarations, in line with how method declarations currently work.

Examples

Suggested Pattern

Below is an example of defining the constraints of this at the accessor site, to determine if an inherited accessor method is valid by the shape of the subclass.

class Base {
   get left<T extends { _left: string}>(this: T) {
       return this._left;
   } 
   get right<T extends { _right: string}>(this: T) {
       return this._right;
   } 
}

...
class Left extends Base {
    _left: string = '';
}
new Left().left; // OK
new Left().right;  // Errors

...
class Right extends Base {
   _right: string = '';
}
new Right().right; // OK
new Right().left; // Errors

...
class Both extends Base {
   _right: string = '';
   _left: string = '';
}
new Both().right; // OK
new Both().left; // OK

This pattern can currently be emulated by converting the accessors into standard methods

class Base {
  getLeft<T extends { _left: string}>(this: T) {
      return this._left;
  } 
  getRight<T extends { _right: string}>(this: T) {
      return this._right;
  } 
}

...
class Left extends Base {
   _left: string = '';
}
new Left().getLeft(); // OK
new Left().getRight(); // Errors

...
class Right extends Base {
  _right: string = '';
}
new Right().getRight(); // OK
new Right().getLeft(); // Errors

...
class Both extends Base {
  _right: string = '';
  _left: string = '';
}
new Both().getRight(); // OK
new Both().getLeft(); // OK

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the requested accessor syntax and this constraints with the existing generic method pattern described in the issue. Investigate how TypeScript currently reports An accessor cannot have type parameters.ts(1094) and how the inherited accessor examples should be checked. Done means the proposed accessor declarations type-check as intended, invalid inherited accesses still error, and existing JavaScript output behavior is unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.