Skip to content

Allow call signatures on classes #619

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

Closed
rjamesnw opened this issue Sep 6, 2014 · 5 comments
Closed

Allow call signatures on classes #619

rjamesnw opened this issue Sep 6, 2014 · 5 comments
Labels
Duplicate An existing issue was already created Suggestion An idea for TypeScript

Comments

@rjamesnw
Copy link

rjamesnw commented Sep 6, 2014

From here: https://typescript.codeplex.com/workitem/2464

I have a possible solution: Just allow this:

class A {
    [index: number]: A; // <- since you can already do this
    (...args): A; // <- why not allow this also?
    constructor(...args) { }
}

Other suggestion by fdecampredon was:

class A {
  (): string {
    //do something
    return 'hello world';
  }
  constructor(): A {
    //do something else
  }
}

could be transformed into that :

function A() {
  if (!(this instanceof A)) {
    //do something
    return 'hello world';
  } else {
    //do something else
  }
}
@ahejlsberg
Copy link
Member

I'm warming up to the idea of allowing call signatures to be part of a class declaration. I think it would be meaningful to consider them a new form of overload of the constructor and validate (by some new rule) that the constructor implementation signature satisfies the contract implied by both kinds of overload signatures (just like we do with regular overloads).

I don't think automatically injecting instanceof checks in the constructor implementation is a good idea. I think it should be up to the programmer to implement the check in whichever way is desired, similar to regular overloads.

@rjamesnw
Copy link
Author

rjamesnw commented Sep 7, 2014

Thanks Anders! Bless your heart. ;)

@fdecampredon
Copy link

There is already a similar issue #183

@RyanCavanaugh RyanCavanaugh added Duplicate An existing issue was already created Suggestion An idea for TypeScript labels Sep 8, 2014
@RyanCavanaugh
Copy link
Member

Thanks for the pointer to 183 -- closing as duplicate.

@rjamesnw
Copy link
Author

rjamesnw commented Sep 8, 2014

Ok, thanks. I did search for an existing request, and nothing came up, not sure how I missed it.

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants