-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Exposing type and object from class factories. #13798
Comments
You can almost accomplish this now, but with an extra empty prototype in the prototype chain, if you do: export class TaggedPoint extends Tagged(Point) {} |
Yes, but that's not user friendly... for a library it doesn't make sense... |
why not: export const TaggedPoint: Constructor<Tagged> & typeof Point = Tagged(Point);
export type TaggedPoint = Tagged & Point; |
a class represents 1. a value, the constructor function, and 2. a type, which is the instance type, you can get the same behavior by exporting a value and a type with the same name. lib.d.ts does this for most declarations e.g. |
@mhegazy Yes, I know that, this is what I demonstrated in the samples. We now have class factories that the developer need to manually export when using a class factory function... I can live with the library author doing it... but if the developer has to, its not pretty :) |
That's even more of an issue when the mixins are multilpe.. export const MyClass = Mixin(A, B, C, D); Now you also need to export this type... |
This is not specific to mixins, the issue is that there is no convienient way to refers to the return type of a function or a constructor. #6606 Is the issue tracking this. |
@mhegazy that's a great issue, I hadn't seen it before. With something like #6606 we could write: const M = <T extends Constructor<{}>>(superclass: T) => class extends superclass {}
type M = typeof M(Object); and not have to duplicate the class expression in a named interface. That's a big improvement. |
@mhegazy #6606 still requires the extra type declaration. Now that class factories can behaves as classes, the only missing piece is how to expose the runtime + design time result natively as if they were declared as classes. export class User = RestMixn() Maybe making it simple export const User = RestMixn() If RestMixn() returns a class, automatically set the type as if export type User = ...; Was done by the developer.... |
We really do not like introducing new syntax that can conflict with ES committee future direction (see design goal #4). We do not have a similar issue with the type space though. I think you do not need to expose additional types, you just |
@mhegazy I agree on the design goal. But,
I understand the limitation, i'm trying to find a solution. What about internal logic: export const User = RestMixn() Seeing that |
A |
:) |
As we are talking about functions that are called to produce classes parametrically, when it comes to exporting the resulting values along with their types in a convenient and discoverable manner, I think it could be good to look at something like #13231. Since these classes are imperatively defined, exporting them seems like it may well fall under the domain of implementing a module interface. |
New runtime |
Following the great mixin implementation by @ahejlsberg (#13743)
Having a mixin function returns a class is powerful but requires manual type declaration.
We can't use
TaggedPoint
as a type, unless we:Using classes we can
Which exposes the type but forces the developer to
extend
theCustomer
class.My feature request is allowing:
Which will be just sugar for:
Maybe even:
but that's not the main point...
The motivation for this is a more robust mixin mechanism that revolves around the base class and allow mixin's to know about it, using generic it provides powerful combinations.
So I can have
And the return type can have members from Mixin1 knowning about Base, using interfaces one can even set static members of a mixin to return T which is the instance of
RestMixin(Base, Mixin1, Mixin2)
all together.The text was updated successfully, but these errors were encountered: