Skip to content
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

WIP: Polymorphic "this" for static members #29484

Conversation

jameskeane
Copy link
Contributor

This PR is a work in progress to solicit feedback on the proposed solution, please do not merge or use this branch.

This commit attempts to make progress towards #5863. It is not a comprehensive solution, but it does provide a way for a library writer to 'opt-in' and provide an interface with a static polymorphic this.

One of the difficulties with #5863, is how to make it clear which this the this type refers to, and the issue of the static and instance type mixing in the same declaration block.

This sidesteps those issues, by only providing a static polymorphic this when using an interface in the extends position. This makes the meaning of this very clear and it's explicit which side the this type falls on; there is no mixing.
i.e:

interface InstType {
  member: this;    // instance this
}

interface CtorType {
  new(): InstType;
  poly_prop: this; // static this
}

declare var X: CtorType;
class Derived extends X {
  ex1() {}
  static ex2() {}
}

const inst = new Derived();
inst.member.ex1();         // instance polymorphic
Derived.poly_prop.ex2();   // static polymorphic

WIP

The PR is not finished. There are a few situations that need to be resolved and tests need to be written if it's decided that this is an acceptable solution.

(Potentially) fixes #5863

This commit attempts to make progress towards microsoft#5863. It is not a
full solution, but should be enough for library writers to use a
polymorphic this for static members.

Consider the following:
```typescript
  interface StaticShape {
    new(): {};
    poly_prop: this;
  }

  // ...

  class Derived extends (X as StaticShape) {

  }
```

One difficultly with attempting to implement microsoft#5863, is how to
make it _clear_ which `this` the `this` type refers to. This
sidesteps the issue, by making it *explicit* which side the
`this` type falls on.
Somehow got missed in the original commit. Need to check that the
base constructor symbol is an interface _before_ calling
`isThislessInterface`.
@RyanCavanaugh RyanCavanaugh added the Experiment A fork with an experimental idea which might not make it into master label Jan 25, 2019
@sandersn sandersn added this to Bête Noire in Pall Mall Jan 29, 2020
@benrbray
Copy link

benrbray commented Sep 3, 2020

Has this experiment been abandoned, or has there been more progress elsewhere?

Copy link

@natasha1007 natasha1007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔

@sandersn
Copy link
Member

This experiment is pretty old, so I'm going to close it to reduce the number of open PRs.

@sandersn sandersn closed this May 24, 2022
@typescript-bot
Copy link
Collaborator

The TypeScript team hasn't accepted the linked issue #5863. If you can get it accepted, this PR will have a better chance of being reviewed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experiment A fork with an experimental idea which might not make it into master
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Polymorphic "this" for static members
6 participants