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

Recognize 'constructor' as a special property when classes implement interfaces #26398

Closed
4 tasks done
todor2810 opened this issue Aug 12, 2018 · 4 comments
Closed
4 tasks done
Labels
Duplicate An existing issue was already created

Comments

@todor2810
Copy link

todor2810 commented Aug 12, 2018

In this code snippet:

interface C1Constructor {
  new (): C1Instance<C1Constructor>;

  // Static properties ...
}
interface C2Constructor {
  new (): C2Instance<C2Constructor>;

  // Static properties ...
}

interface C1Instance<Constructor> {
  constructor: Constructor;

  // Instance properties ...

  // Prototype properties ...
}
interface C2Instance<Constructor> extends C1Instance<Constructor> {
  // Instance properties ...

  // Prototype properties ...
}

class C1 implements C1Instance<C1Constructor> {}
class C2 extends C1 implements C2Instance<C2Constructor> {}

currently errors are thrown in the last 2 lines.[1][2]

I propose that TypeScript recognizes 'constructor' as a special property when classes implement interfaces so that the above code will be valid.

The benefit of this will be that we will be able to check both the instance and static side of a class against interfaces.


[1]

[ts]
Class 'C1' incorrectly implements interface 'C1Instance'.
Types of property 'constructor' are incompatible.
Type 'Function' is not assignable to type 'C1Constructor'.
Type 'Function' provides no match for the signature 'new (): C1Instance'.

[2]

[ts]
Class 'C2' incorrectly implements interface 'C2Instance'.
Types of property 'constructor' are incompatible.
Type 'Function' is not assignable to type 'C2Constructor'.
Type 'Function' provides no match for the signature 'new (): C2Instance'.

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. new expression-level syntax)
@todor2810
Copy link
Author

todor2810 commented Aug 13, 2018

I would like to point out it is currently possible to check the instance and static side of a class against interfaces via a different approach:

interface C1Instance {
  // Instance properties ...

  // Prototype properties ...
}
interface C2Instance extends C1Instance {
  // Instance properties ...

  // Prototype properties ...
}

interface C1Constructor {
  new (): C1Instance;

  // Static properties ...
}
interface C2Constructor {
  new (): C2Instance;

  // Static properties ...
}

type C1 = C1Instance;
let C1: C1Constructor = class {};

type C2 = C2Instance;
let C2: C2Constructor = class extends C1 {};

let c1: C1 = new C1();
let c2: C2 = new C2();

@RyanCavanaugh
Copy link
Member

Duplicate #1263 ?

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 13, 2018
@ghost
Copy link

ghost commented Aug 13, 2018

Which links to #420 which is presumably still accepting PRs

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants