nartc / automapper-transformer-plugin

Typescript Transformer Plugin for @nartc/automapper

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add supporting optional and nullable properties.

MaksHladki opened this issue · comments

Hey, I continue testing the plugin :) some additional moments from my side.
In all cases, I've used your webpack example

ts-loaded can't understand nullable properties.

Let's modify the example to use primitive nullable property there.

export class Address {
  street!: string;
  zipCode!: string | null;
}

export class AddressVm {
  formattedStreet!: string;
  zipCode!: string | null;
}

If you build the app, you will see an exception "TypeError: Cannot read property 'transformFlags' of undefined".
The same effect with classes. Let's mark the profile property as nullable.

export class User {
  firstName!: string;
  lastName!: string;
  profile!: Profile | null;
  addresses!: Address[];
}

export class UserVm {
  first!: string;
  last!: string;
  full!: string;
  profile!: ProfileVm | null;
  addresses!: AddressVm[];
}

Build exception "TypeError: Cannot read property 'transformFlags' of undefined".

ts-loaded can't understand optional properties.

I've added test property to the User model. The property can be marked as undefined. If you build the app, you will see an exception "TypeError: Cannot read property 'transformFlags' of undefined".
The same situation with class types.

export class User {
  firstName!: string;
  lastName!: string;
  profile!: Profile;
  addresses!: Address[];
  test?: number;
}

export class UserVm {
  first!: string;
  last!: string;
  full!: string;
  profile!: ProfileVm;
  addresses!: AddressVm[];
  test?: number;
}

If I used the decorator, all examples would work well.
It would be great if you could find a solution to how to fix these issues easily... and sorry for my importunity :))

@MaksHladki The latest release should address all of these. It was about the strict option in tsconfig.json. Turning on strict will provide additional information about a Type of a Node (property).

nullablePrimitive!: string | null;
maybePrimitive?: string;
nullableType!: SomeType | null;
maybeType?: SomeType;
nullablePrimitives!: string[] | null;
maybePrimitives?: string[];
nullableTypes!: SomeType[] | null;
maybeTypes?: SomeType[];

All of these cases have been handled. However, for more complex unions that have different types. Then I'd suggest the AutoMapper consumers to decorate their fields with @AutoMap(). Automated tools have its limit.

Let me know if the latest release works out for you. Reopen the issue (or create new one) if you run into problems. Thanks again.