question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't get Composition to work

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Neos3452commented, Mar 11, 2019

Hey Apidcloud, you are right reflect-metadata is 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.

1reaction
Apidcloudcommented, Mar 11, 2019

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 constructor property in the jsonMembers? You can close the issue I think. I will reopen if I face any other issues!

Thank you for your time!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found