Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
3 of 9 tasks
IT-CASADO opened this issue Apr 9, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@IT-CASADO
Copy link

IT-CASADO commented Apr 9, 2024

Is there an existing issue for this?

  • I have searched the existing issues

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

  • I don't know.
  • @automapper/core
  • @automapper/classes
  • @automapper/nestjs
  • @automapper/pojos
  • @automapper/mikro
  • @automapper/sequelize
  • Other (see below)

Other package and its version

No response

AutoMapper version

8.8.1

Additional context

No response

@IT-CASADO IT-CASADO added the bug Something isn't working label Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant