Nx: @nrwl/react decorators not working

Created on 1 Jul 2020  路  2Comments  路  Source: nrwl/nx

I was toying around with typedjson (wich requires reflect-metadata and the decorator flags in the tsconfig)
but its not working

typedjson.js:179 @jsonMember on MyDataClass.prop1: could not resolve detected property constructor at runtime. Are you sure, that you have both "experimentalDecorators" and "emitDecoratorMetadata" in your tsconfig.json

is what i get. In @nrwl/node it works

Current Behavior

Error message pops in the console

Expected Behavior

That this works.

Steps to Reproduce

  1. Create a @nrwl/react app, install reflect-metadata typedjson , setup allowSyntheticDefaultImports and emitDecoratorMetadata in your tsconfig

  2. ```import 'reflect-metadata';
    import { jsonObject, jsonMember, TypedJSON } from 'typedjson';

import React from 'react';
import ReactDOM from 'react-dom';

import App from './app/app';

@jsonObject
class MyDataClass
{
@jsonMember
public prop1: number;

@jsonMember
public prop2: string;

}

@jsonObject({
initializer: (json: Derived) => {
return new Derived(json.prop3);
}
})
class Derived extends MyDataClass {
@jsonMember
public prop3: number

@jsonMember
public prop4: number;

constructor(prop: number) {
  super();
  this.prop3 = prop;
  this.prop4 = 69
}

method() {
  console.log(this.prop3, this.prop4)
}

}

const serializer = new TypedJSON(Derived);
const object = new Derived(20);
object.prop4 = 420;

const json = serializer.stringify(object);
const object2 = serializer.parse(json);
object2.method();

console.log(object2 instanceof MyDataClass); // true

ReactDOM.render(, document.getElementById('root'));
``` use that in your main.tsx

react enhancement

Most helpful comment

Turns out i had to use some custom babel plugins to make it work

.babelrc

{
  "plugins": [
    "babel-plugin-transform-typescript-metadata",
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    ["@babel/plugin-proposal-class-properties", { "loose": true }]
  ],
  "presets": [
    "@babel/preset-typescript"
  ]
}

All 2 comments

Turns out i had to use some custom babel plugins to make it work

.babelrc

{
  "plugins": [
    "babel-plugin-transform-typescript-metadata",
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    ["@babel/plugin-proposal-class-properties", { "loose": true }]
  ],
  "presets": [
    "@babel/preset-typescript"
  ]
}

what version of nx are you using?

This feature was added in 9.4 by supporting babel.config.js with .babelrc as children per lib/app

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zachnewburgh picture zachnewburgh  路  3Comments

Svancara picture Svancara  路  3Comments

danieldanielecki picture danieldanielecki  路  3Comments

zpydee picture zpydee  路  3Comments

jasedwards picture jasedwards  路  3Comments