-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Allow class inheritance in ambient contexts, even with a base private constructor #18283
Comments
It seems more like a |
@aluanhaddad I don't understand. Do you mean not exposing the base constructor? The compiler would then allow declare class Foo {
}
let x = new Foo(); Or do you mean not exposing the base class, only the derived class? But the base class provides members to multiple inheriting classes. It also serves as a type in its own right.
Not quite. There is such a constructor, but it is unavailable from any Typescript/Javascript code, even from classes which would like to inherit from it. This error makes perfect sense in a non-ambient contex -- if I define a class as But in an ambient context, a class defined as inheriting from the base class can never actually call the |
I did indeed mean not exposing the base class at all, sorry for not being more explicit. with respect to using the base class as a type, why not expose it as an interface or type alias? I definitely see the argument that this is not unreasonable in an ambient context but it seems a bit odd especially since the constructor function will be very much accessible at runtime to any code trafficking in an instance of any class or any constructor function in the hierarchy. I'm probably misunderstanding the code that this would enable. Is it related to using |
@aluanhaddad The use case here is when the Javascript host supplies objects that look and behave like Javascript objects WRT reading/writing properties and calling methods, but not WRT object construction via At runtime, there isn't an actual Javascript constructor function; the underlying object is constructed via whatever mechanisms are available to the language used to develop the host (in the case of the LibreOffice API, this is either C++ or Java).
Because that would allow using a plain Javascript object with the appropriate members when the type is expected. If the API expects a binary C++ created object of type declare class GenericTextDocument {
private typekey: GenericTextDocument;
} then this can be prevented: let x: GenericTextDocument = {
TextTables: '',
CharacterCount: 5
}; which is not the case if |
@zspitz I see thank you for taking the time to explain. |
@RyanCavanaugh @mhegazy Is there any description of the meaning of the various issue labels, such as "help wanted" and "Committed"? |
TypeScript Version: nightly (2.5.0-dev.20170902)
Code
Expected behavior:
Compile without error.
Actual behavior:
error TS2675: Cannot extend a class 'Foo.Bar'. Class constructor is marked as private.
Use case
This would allow declaring the types of host-supplied objects, with the following features:
The text was updated successfully, but these errors were encountered: