JohnWeisz/TypedJSON

Get datatype of properties

Opened this issue · 3 comments

s888 commented

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

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 commented

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.

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.