Can't get Composition to work
See original GitHub issueI’m getting the following error, but I don’t know why:
@jsonMember on Person.anotherClass: ReflectDecorators is required if no 'constructor' option is specified.
Here’s the example, where I am trying to use composition. Based on the tests, I added the knownTypes, but it’s still not working:
import { jsonObject, jsonMember, jsonArrayMember, TypedJSON } from "typedjson";
import "reflect-metadata";
@jsonObject()
class AnotherClass {
private _arr: Array<string> = ["str1", "str2"];
constructor() {}
@jsonArrayMember(String)
get arr(): Array<string> {
return this._arr;
}
set arr(arr: Array<string>) {
this._arr = arr;
}
methodB(): string {
return "something";
}
}
@jsonObject({
knownTypes: [AnotherClass]
})
class Person {
constructor(name?: string) {
this._name = name || "some default name";
}
private _name: string;
@jsonMember
public anotherClass: AnotherClass = new AnotherClass();
@jsonMember({ constructor: String })
get name() {
return this._name;
}
set name(name: string) {
this._name = name;
}
static testSerialization() {
let serializer = new TypedJSON(Person);
let object = new Person("LLP");
console.log("original", object);
let json = serializer.stringify(object);
console.log(json);
let object2 = serializer.parse(json);
console.log("restored", object2);
console.log("instance of", object2 instanceof Person); // true
}
get lang() {
return "JavaScript";
}
}
I have installed reflect-metadata both locally and globally, but doesn’t seem to affect the result.
Any ideas? thanks!
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Unable to Export After Effects Composition using Adobe ...
Workaround · In Adobe Media Encoder, select Media Encoder CC > Preferences > General. · Under Video Rendering, change the Renderer to Mercury ......
Read more >Unable to add to Media Encoder queue from After Effects
I was trying to use Media Encoder to work around an issue in After Effects where it won't play 4K video with audio...
Read more >Measuring Body Composition Doesn't Work (GW4, 44mm)
This morning measuring my body composition using the Galaxy Watch 4, 44mm was a breeze without needing to tighten my strap.
Read more >Trim Compositions Based on In and Out Points
STEP 2: TRIM COMP TO WORK AREA ... Keyboard Shortcut in After Effects: ... Once you defined the work area go to the...
Read more >Body+ - My scale doesn't measure body composition. What ...
If your scale doesn't measure body composition, perform the following steps: Make ... However Withings is not aware of any contraindication to the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hey Apidcloud, you are right
reflect-metadatais basically just to skip the need to type a constructor every time. It is completely optional.Funny thing, in typescript compiler there is a big if statement that changes the format of emitted metadata which will break all the code that is using the current format. I am not sure how they are going to handle that since the present version is marked as experimental. Let’s just hope they will add a flag for that.
Yeah, I don’t think the metadata is in the output. Couldn’t find those functions you mentioned before. How can I try this without the
reflect-metadata? What else would I need to set?It works with strings.
Edit: I think I was having some cache issues in the browser, as it is now working with arrays too. So the only downside is to specify
constructorproperty in thejsonMembers? You can close the issue I think. I will reopen if I face any other issues!Thank you for your time!