JohnWeisz/TypedJSON

No example for usage with ReflectDecorators

Closed this issue · 3 comments

I found no examples on how to use this library with ReflectDecorators, and I couldn't make it work. I installed that package in my node project, imported it (I checked and it was in the 'global' object) yet on running TypedJSON still required the 'constuctor' option in the @jsonMember decorator. I tried to add a @Reflect.metadata('',Class) decorator but I don't know how to configure that.

Please provide a short example in the Readme.md and/or in the Wiki

Hi @AlexAegis,

Manual Reflect.metadata calls shouldn't be required, if reflect-metadata is imported globally then @jsonMember should work out-of-the-box. The readme specifies that:

TypeScript needs to run with the experimentalDecorators and emitDecoratorMetadata options enabled.

Was this enabled for your project?

Hi AlexAegis, you can look at the configuration in the repository, it uses reflect in the tests. Remember that you have to transpile with tsc to get the reflect decorators automatically there. Basically this config should be enough, we use ts-node but it loads it automatically.
https://github.com/JohnWeisz/TypedJSON/blob/master/tsconfig.json

Thank you for the quick response, although none of these were the source of my problem, it gave me an idea and it turned out to be right:

I assumed importing reflect-metadata before using the decorators from this package is enough, but actually I have to import them before that import:

// bad
import { jsonObject, jsonMember, toJson } from 'typedjson';
import 'reflect-metadata';

@jsonObject
@toJson
export class Simple {}

// good
import 'reflect-metadata';
import { jsonObject, jsonMember, toJson } from 'typedjson';

@jsonObject
@toJson
export class Simple {}