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

error TS2385: Overload signatures must all be public, private or protected. Why? #7577

Closed
0815fox opened this issue Mar 18, 2016 · 5 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@0815fox
Copy link

0815fox commented Mar 18, 2016

public myMethod();
private myMethod(Data?:T,FromRevision?:number,TilRevision?:number);
public myMethod(Data?:T,FromRevision:number=0,TilRevision:number=0) {
  ...
}

gives:

error TS2385: Overload signatures must all be public, private or protected.

What is this restriction needed for? Of course a possible workaround is an internal helper function:

private myMethodInternal(Data?:T,FromRevision:number=0,TilRevision:number=0) {
  ...
}

public myMethod() {
  this.myMethodInternal();
}

But it is just unnecessary work and unnecessary function calls (performance, when called often).
Even worse, when i try to do this on a constructor, it complains:

error TS1089: 'private' modifier cannot appear on a constructor declaration.

Why not? I wanted to have a constructor, which is only accessible by methods inside the class and allows to specify additional parameters to the constructor, which would else hold their default values. Of course, the workaround above also works in this situation.

@mhegazy
Copy link
Contributor

mhegazy commented Mar 18, 2016

Constructor visibility is now supported in typescript@next. please see #6885

As for visibility on signatures, the type system is TypeScript is structural, it is not clear what it means to compare two types with signatures that might not match in visibility.

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Mar 18, 2016
@aluanhaddad
Copy link
Contributor

Since there is no function overloading in JavaScript, there is only ever one method to call. From an api design perspective, your usecase makes sense, but as visibility modifiers in TypeScript are essentially a design time illusion, although there may possibly be support in a future version of ECMAScript,

@aluanhaddad
Copy link
Contributor

Accidentally hit enter:
From an api design perspective, your usecase makes sense, but as visibility modifiers in TypeScript are essentially a design time illusion, although there may possibly be support in a future version of ECMAScript, I think this restriction makes sense.

@0815fox
Copy link
Author

0815fox commented Apr 26, 2016

Are you sure? I mean, there is lots of stuff, which is just a design time illusion. What I would like to achieve with that is, that tsc checks, that I can only call public signatures from outside of a class, while I can also call private signatures from inside the class and protected ones from inside a child class.

@mhegazy
Copy link
Contributor

mhegazy commented May 20, 2016

It is not clear there is much value for such feature vs the complexity added to the language and the implementation.

@mhegazy mhegazy closed this as completed May 20, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants