devkit icon indicating copy to clipboard operation
devkit copied to clipboard

[swc-plugin-angular] Functional APIs (e.g. `input()`) are broken if a property is declared in the constructor

Open yjaaidi opened this issue 1 year ago • 1 comments

This doesn't work because the input is not decorated:

class MyCmp {
  title = input();
  constructor(private myService: MyService) {}
}

This happens because this is parsed by SWC as:

class MyCmp {
  title;
  private myService;
  constructor(myService) {
    this.myService = myService;
    this.title = input();
  }
}

We have to detect functional APIs in the constructor.

Meanwhile, the workaround is to use the inject() function which is the approach I'd recommend in general.

yjaaidi avatar Apr 04 '24 09:04 yjaaidi

got this also: looks like problem in ts config "useDefineClassFields" where by default is true, but need false for proper working (in my case). I got some issue about that https://github.com/swc-project/swc/issues/6985 but when I try to set

.swcrc

{
  "$schema": "https://swc.rs/schema.json",
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": false,
      "decorators": true
    },
    "transform": {
      "useDefineForClassFields": false,
      "legacyDecorator": true,
      "decoratorMetadata": true
    },
    "target": "es2022"
  },
  "module": {
    "type": "commonjs"
  }
}

Also I try to add that in swcAngularPreset inside npm package "@swc-angular" but it's not working.

console.error
      NG0303: Can't set value of the 'addresses' input on the 'AddressComponent' component. Make sure that the 'addresses' property is annotated with @Input() or a mapped @Input('addresses') exists.

pumano avatar Sep 02 '24 07:09 pumano