nartc / mapper

🔥 An Object-Object AutoMapper for TypeScript 🔥
https://automapperts.netlify.app/
MIT License
984 stars 87 forks source link

Unexpected typescript error "Cannot assign an abstract constructor type to a non-abstract constructor type." #614

Open IT-CASADO opened 7 months ago

IT-CASADO commented 7 months ago

Is there an existing issue for this?

Describe the issue

I get an unexpected typescript compile error:

"Cannot assign an abstract constructor type to a non-abstract constructor type."

You can find a stackblitz here: https://stackblitz.com/edit/typescript-playground-6bnct5?file=index.ts

Models/DTOs/VMs

enum VehicleType {
  car = 'car',
  bus = 'bus',
}

class VehicleDto {
  name: string;
  type: VehicleType;
}

abstract class Vehicle {
  constructor(public name: string, vehicleType: VehicleType) {}

  static create(name: string, vehicleType: VehicleType): Vehicle {
    switch (vehicleType) {
      case VehicleType.bus:
        return new Bus(name);

      default:
        return new Car(name);
    }
  }
}

class Car extends Vehicle {
  constructor(name: string) {
    super(name, VehicleType.car);
  }
}

class Bus extends Vehicle {
  constructor(name: string) {
    super(name, VehicleType.bus);
  }
}

Mapping configuration

import { createMapper, createMap, constructUsing } from '@automapper/core';
import { classes } from '@automapper/classes';

const mapper = createMapper({
  strategyInitializer: classes(),
});

createMap(
  mapper,
  VehicleDto,
  Vehicle,
  constructUsing((source) => Vehicle.create(source.name, source.type))
);

Steps to reproduce

https://stackblitz.com/edit/typescript-playground-6bnct5?file=index.ts

Expected behavior

No typescript compile errors with the given code.

Screenshots

image

Minimum reproduction code

No response

Package

Other package and its version

No response

AutoMapper version

8.8.1

Additional context

No response