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

Get datatype of properties #123

Open
s888 opened this issue Feb 4, 2020 · 3 comments
Open

Get datatype of properties #123

s888 opened this issue Feb 4, 2020 · 3 comments

Comments

@s888
Copy link

s888 commented Feb 4, 2020

Hi,

I there a built-in way to get datatype of @jsonMember properties?
Or will I have to write custom decorators for that?
Also, does it support enum? I am asking so since I couldn't find any example for the same.

Thanks in advance

@Neos3452
Copy link
Collaborator

Neos3452 commented Feb 5, 2020

Hey @s888, could you describe in more details what you would like to achieve? If you are using TypedJSON with 'reflect-metadata' you could check its documentation do just that.

Unfortunately enums are not supported natively because of how typescript represents enums (and when using reflect we just get Object prototype). However, if you are using const enums with string values providing String for the decorator should work, but specific values will not be validated.

@s888
Copy link
Author

s888 commented Feb 5, 2020

Hey @s888, could you describe in more details what you would like to achieve? If you are using TypedJSON with 'reflect-metadata' you could check its documentation do just that.

Unfortunately enums are not supported natively because of how typescript represents enums (and when using reflect we just get Object prototype). However, if you are using const enums with string values providing String for the decorator should work, but specific values will not be validated.

Thank you for the reply. I want to dynamically create Graphql mutations from the class properties. But for that I need the data type of the properties. Say for example, for nested object if the property is a class I will need the name of the class so that I can pass it in the mutation. One way is I can create decorators like

function pType(target : any, key : string) {
var t = Reflect.getMetadata("design:type", target, key);
console.log(${key} type: ${t.name});
}
class User {
@ptype()
@jsonMember
id: number
}

But I was wondering if typedJson has an inbuilt way to find the type of property.

@Neos3452
Copy link
Collaborator

Neos3452 commented Feb 6, 2020

Ok, but do you want to use that to “serialize” an object or just save it somewhere else? If the former, ie. use it in a custom serializer then I was planning to implement that at some point so I could just do it now, but if the latter then I would say that this is not what TypedJSON should be used for.

Internally we either use reflect metadata as in your example or expect it to be provided in the decorator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants