-
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
Decorator Metdata: expose array element type #7169
Comments
looks like a duplicate of #4521 |
Hi Mohamed, |
thanks. changing the title to reflect the request. |
Thanks a lot! |
Running into the same issue here. Would be very helpful if we could get to the full nested type information. |
This problem apparently still persists. And it really is a dealbreaker for one of my current projects. In an attempt to implement a workaround of my own, much like and inspired by @mindruptive , I created a fork of the TS Compiler, which currently achieves JS outputs along the lines of e.g.:
or
for (nested) Arrays as well as lessen the condensing of UnionTypes like so:
I realize that this workaround, which is sufficient for said project, creates a breaking change to the behavior of metadata, as (e.g.) the return parameter of Reflect.getMetadata('design:type', target, key?) is no longer guaranteed to be a constructor. |
It seems as though there is actually some progress regarding this issue. From what I can tell after looking at the current TSC, there seems to be a modified implementation of the metadata decorators (and two separate instances of said decorator emitters (
I'd suggest modifying the output to emit:
This approach is obviously more elegant than building custom constructor functions (with or without a class or object mantle) to inject the elementType into. |
I should note that Decorators have a new proposal in TC39 that should facilitate adding additional meta-data fields such as this one. I would expect TypeScript to implement the new proposal in the coming releases. |
Are there any informations when design:typeinfo will be available? |
No updates since #7169 (comment). |
@mhegazy Do you know of any workaround that can be used in the meantime? This is a major pain for a couple of our projects. |
I think a workaround is only possible by defining the type in the decorator manually. |
@DominicBoettger @mhegazy @abierbaum
Finally to use the generated metadata in my property decorator:
From a compiler designer's perspective this might be dirty. TypeScript is great and the static type info makes any kind of schema-generation much easier. This slight enhancement even more so. :-) |
|
+1 to avoid workarounds like https://github.com/edcarroll/ta-json#jsonelementtypetypefunction |
and time goes by so slowly, ... |
+1 |
Hi, any news? |
Any progress after more than one year? Thanks :D |
Excellent question! :-) |
@mhegazy Can you give us some updates on this? What's the next step to add this on the master branch(beside the actual merge request)? Maybe @thomas-mindruptive cand help with this. |
@jcwolf I can provide the "patch-code" I have posted above. But I think it's more of a general discussion within the TS team if and how they want to do this. It would be easy (technically) for the transpiler to decorate additional type-info. Maybe they are waiting for the EcmaScript-specs for these features. (Which would be boring because TS was always ahead! :-)) |
And the years go by: > 3 |
+1 |
This would be really awesome... Been waiting on this feature awhile. |
I feel like emitting generic type information is the next step towards having a strongly reflective language. At the same time I'm sure there must be technical/design dilemmas that haven't been covered in this thread. Either way, a comment from the typescript team here would be very welcome |
Any update on this? Would be nice to emit something like the following structure: interface TypeMetadata {
name: string;
value: any;
parameters: Array<TypeMetadata>;
} So, for example, the result for {
name: 'Map',
value: Map,
parameters: [
{ name: 'String', value: String, parameters: null },
{ name: 'MyInterface', value: Object, parameters: null },
],
} To make this even more unambiguous, perhaps it can be useful to also include a path parameter with the value of the absolute path to the file where a custom type like MyInterface is defined. This way people can use this information at runtime to accurately get types from metadata. |
Any progress after more than one year? |
Looking forward to updates. XD |
I wish I am the last one to post this question: Any update? |
I've wrote a small example showing the use of decorators for request validations. Now i came across this issue. example attr declaration @prop()
foo: bar; check if (Array.isArray(obj.foo)) {
obj.foo.forEach((elm) => {
if (elm instanceof bar === false) {
return false
}
}
} But then again i would rather prefer this: example attr declaration @prop()
foo: bar[]; decorator-factory function prop(): any {
return function (target : Object, key : string) {
let type = Reflect.getMetadata("design:type", target, key);
console.log(type.isArray()) // true
console.log(type.constructor.name) // "bar"
}
} |
For everyone else who is, like me, wondering why this isn't getting any attention, a Redditor had this explanation for me:
With the disclaimer that this is just him reading the tea leaves. |
@NSExceptional That's a bit sad but I know what the TS team feels and they are right. It's so hard to do something that doesn't have a spec. I hope decorators will get specced soon because they are super awesome and save you from writing a lot of code and makes it look really expressive. |
Sometimes its better to ship implementation and made spec out of it based on real world needs. Decorators are super usefull and saves tons of code. |
It's a pity. TypeScript used to lead the way and provide new functionality, which was not available in JS. |
Would love to see this happen. |
Please give us this feature. |
+1 |
Are their update on this? |
So, it's been seven years since initial request... Anyone found a solution for this? |
Implemented a transformer using ts-patch that can provide some workaround.. |
TSC 175, target ES6, Node 5.6
Sample class with decorated properties:
Property decorator:
Current behaviour:
When the decorator gets called for property "friends", "type" is "Array" and the captured type ("User") is lost.
Expected behaviour
"type" should contain the captured type ("User"), too
Following very dirty work-around does the job: I modified tsc's method "emitSerializedTypeNode":
Best
Mind
The text was updated successfully, but these errors were encountered: