-
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
TS Proposal : abstract class expression, generic metadata, constructor typing, abstract class decorators, abstract members decorators #20887
Comments
The first scenario has implemented in typedin with existing TypeScript features. |
@PFight , I've a look at typedin and :
The above proposal aim to bring the #3628 problem down to abstract classes. It does not allow injecting pure abstract interfaces. As stated at the beginning, by no mean Interfaces should have runtime-life as far as this is not strongly considered at ECMA International. DOM spec on itself have about a thousand interfaces and none come to exists at runtime. While interfaces exist not at runtime, abstract classes do. And the suggest is to shift our concerns there. |
One issue is that Besides, you want is a framework, like nest, or angular. You should check them out. |
@michaeljota I know. Please check my answer above to @PFight. Beside, I've used enough frameworks and am now writing mine. In fact, I want it lighter, faster, its components usable independently or packaged, to address the sole purpose of a framework (not a all-in-one solution), hybrid as simple as (let's say what, JSON), as powerful as (why not) spring... But in TS. I'll soon push the latest changes at @squall-io/magnitude-ts and @squall-io/magnitude under MIT. Nevertheless, my suggest concerns TS alone. I just picked some examples to show what will become possible and how these TypeScript features will help to it. |
I read them. I read the post actually, because it sounds interesting. But at the end, this is a language, and as such there is no reason to add a DI framework to it, but build a DI framework with it. I don't think there is a language that, as language, have a DI framework. There is C#, and the .NET Framework, but C# as is, does not allows DI per se. |
@michaeljota, DI was a suitable use case to justify the need of these features and more... To show that some enforced constraints (which I still believe were not needed: I open this issue hoping in either implementation or explanation why this isn't possible) limited the possibilities of this (superset) language. By no mean am I suggesting DI in typescript-core : to be sure, nowhere does the word |
Literally in the title: |
@michaeljota : finally got you. I'm updating |
This is already supported today.
The abstract method does not really exist at runtime. so there is nothing to decorate really. it is not clear what decorating an abstract method really means anyways.
That is a runtime type system request, and it is outside the scope of the TS project. you might find the discussion in #3628 relevant.
Class expression decoration is tracked by #7342
Tracked by #17572
|
@mhegazy , Thanks for the elaborated answer. Another project has been holding all my attention these past few months and I have not keep updating how TS is moving. Decorator support for abstract class is great improvement. Thanks to TS team and contributors ! PS: This issue may now deserve to be closed ! |
Hi @mhegazy , Think back about what I had in mind when I suggested decorators for abstract members... I agree when you say
But as for...
It means I want to attach some metadata about this. A Use-CaseLet me borrow it from Java world where the following is possible... and might be meaningful when it comes to TypeScript: public interface PetRepository extends JPARepository< Pet >
{
@JQL("SELECT * FROM Pet WHERE id = { id } AND specie = '{ specie }' ")
public ArrayList< Pet > complexQuery(id: Long, specie: String);
} (Something to the likeness of what is shown on this web page) Having decorators on an abstract member like this in TypeScript may let the decorator provide specific implementation, relying for instance, on a query builder. |
@SalathielGenese I've been following this issue, as I though it may be useful for certain scenarios. However, what I understand, if that you would be able to decorate a interface or an abstract member, what should the Typescript compiler decorate at the end? The implementation of the member? Then, maybe what you mean is some kind of mixin pattern, but with decorators. |
@michaeljota TypeScript MAY consider decorating abstract classes members and such decorators might help provide a proper implementation in the flow of IoC (Inversion of Control) / DI (Dependency Injection). In the days I opened this proposal, I was working a tiny IoC/DI framework along with ORM features, almost as much powerful as what Spring Framework does in Java. I came about thinking that such features will enable some kinda magic in javascript through the TypeScript superset. If one may have a generic I'll be continuing that project ASAP.
Well, just provide decorator with metadata as for implementation members - then decorator may cache those metadata and give them to the injector at DI time for implementation. I tried to answer your question most clearly, let me know if some shadows are still there. Kind Regards! |
Neither, nor interfaces, nor abstract class members, live at runtime. And what I ask was about that, giving the case as not interfaces or abstract members live at runtime, what should the compiler decorate? |
Abstract class (abstract for TypeScript) do live at runtime (full class for JS) as shown on this playground page. Again, this in-haste example shows that abstract class is complete at runtime. Now, I'd expected I could decorate the abstract method. |
Yes, abstract classes live at runtime, but you are talking about decorating abstract members, and they don't. |
I guess you mean TypeScript doesn't decorate abstract members... And that's exactly my feature request! |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
i also want this feature to be supported. maybe we could give it a config in |
TypeScript Issue-Proposal
TypeScript 2.6.2
What follow are proposals for TypeScript. If follow a well know discussion, rather official #3628. It propose handling [1] abstract class decorators, abstract members decorators, generic metadata, and [2] constructor typing, and abstract class expression
[1]: Abstract class decorators, abstract members decorators, generic metadata
As to me, it is clear that by no mean should Interface exist at runtime as far as ECMA doesn't consider it worth runtime-life. However, need for this feature is more and more demanded, the reason is simple for me: more poeple are moving from JAVA to TypeScript (steping over Javascript) and so will it be with Kotlin, which they bitterly defend already: I wonder if they face the same challenging Interface DI when transpiling to Javascript.
Initial scenario
Writing such code is very difficult, not to say impossible in typescript, at least, using the official transpiler. Keeping in mind that no
interface
should be decorated at the moment, my suggest should make this possible :Scenario -001
In this case, generated metadata following
@CustomCrud()
should describe methods and attibutes fromCrudLike
which the abstract class implements.Scenario -002
This second senario just improves the first one, enabling us to write things like :
A word to conclude : the proposal
It come up that this would be possible :
abstract class
will support decoratorsdecorator
could be applied to abstract members of abstract classesmetadata
will describe even generic types (If TS support complex generics, it means they can be modeled and represented, thus supported)I really wonder why such constraints were first enforced !
[2]: constructor typing, and abstract class expression
TypeScript documentation proudly exhibit this fact :
Please, stop when I am wrong. This statement also implies that the following are identical :
Scenario -001 : Constructor typing
The decorators
fn_001
andfn_002
where type enforced to work withType
and are now traped to accept evenAnotherType
. If one say this design is backed by no logic, I answer that typing was introduced for best practice and early violation detection. The question that springs up now is [W]hat typescript construct will ensure for sure that our constructor is forType
andType
only.Scenario -002 : abstract class expression
The following scenario is a simplified image of my actual case, which is much more complicated.
Take note that even
fn_001
SHOULD NOT implementidentifier
accessor as it is not its reponsibility: another decorator ensures it. As some may have seen,abstract
keyword isn't allowed in class expression which is a limitation.The following isn't a solution !
What follow works well, BUT a 'one decorator - all purpose' isn't a great design.
However, the following calls are rejected : ask me why ?
In fact, none of the above
fn_001
andfn_002
works when called.However, the following works great :
This somehow take us to the question in scenario -001 : constructor typing.
Nonetheless, I was able to come up with something that works :
The following implementation leads to unsuspected results : everything works well.
This encouraged me to go a little further :
If, Ôh if abstract classes were allowed in class expressions, I could have wrote something like... Being confident of its validity at rutime.
A word to conclude : the proposal
The proposal is as follow :
Support of
abstract class
in class expressionBetter constructor typing:
type Constructor<T> = typeof T;
(because T is used here as a value !?)By the way, it should be considered adding a
name
attribute to constructor types (i.e{new()...}
and its interface avatar). So as to ease the following.Summary
The proposals are summed as follows :
abstract class
AbstractedPropertyDescriptor
or a fourth parameters as boolean (why not? let's be crazy this far)metadata
that describe up to generic typesabstract
class expressiontype Constructor<T> = typeof T;
(because T is used here as a value !?)name
attribute to constructor types (i.e{new()...}
and its interface avatar).The text was updated successfully, but these errors were encountered: