Ts-morph: Decorator factory in structure

Created on 29 Oct 2018  路  6Comments  路  Source: dsherret/ts-morph

I just tried adding decorators, and they show up as decorators rather than factories (missing brackets). I know there's a setIsDecoratorFactory but when working with structures it'd be nice if they also work.

I wouldn't mind helping if you could point me in the right direction because right now creating these decorators is a bit of a pita in my implementation.

question

All 6 comments

Update:

Here's an example of what I've been able to generate vs what I want to generate:

What I get:

import { entity, autoFields, field, Mapping, primary } from 'wetland';

@entity
@autoFields
export class Profile {
  @field({ type: 'dateTime', defaultTo: Mapping.now() })
  public createdAt: Date;

  @field({ type: 'text' })
  public moto: string;

  @field({ type: 'boolean' })
  public available: boolean;

  @field({ type: 'string' })
  public avatar: string;

  @field({ type: 'number' })
  @primary
  public foo: number;
}

What I want:

import { entity, autoFields, field, Mapping, primary } from 'wetland';

@entity()
@autoFields()
export class Profile {
  @field({ type: 'dateTime', defaultTo: Mapping.now() })
  public createdAt: Date;

  @field({ type: 'text' })
  public moto: string;

  @field({ type: 'boolean' })
  public available: boolean;

  @field({ type: 'string' })
  public avatar: string;

  @field({ type: 'number' })
  @primary()
  public foo: number;
}

Sorry that's not so clear. The way to do it is to provide an empty arguments array:

propertyDec.addDecorator({
    name: "primary",
    arguments: [] // this
});

I think I love you. Don't tell my wife.

I had a good chuckle about that earlier.

Just wanted to follow up on this now that I have more time to explain for anyone reading in the future...

I thought about doing this:

propertyDec.addDecorator({
    name: "primary",
    isDecoratorFactory: true
});

But then it would be strange if someone provided something like the following:

propertyDec.addDecorator({
    name: "primary",
    isDecoratorFactory: false,
    arguments: ["5"]
});

I could have also added another type that doesn't have the arguments array when isDecoratorFactory is false, but I didn't want to complicate the structure types too much. So I opted for the empty arguments array, which I believe makes sense, but it's unfortunately not as discoverable.

You're right, it does make more sense. Perhaps renaming it to argumentsOrFactory and allowing true to be passed in would make it more discoverable. Otherwise adding it in a doc somewhere?

@RWOverdijk thanks! I updated the js docs on the structure.

Was this page helpful?
0 / 5 - 0 ratings