jorgebodega / typeorm-factory

:seedling: A delightful way to use factories in your code.
https://www.npmjs.com/package/@jorgebodega/typeorm-factory
MIT License
29 stars 10 forks source link

Type *Entity* is not assignable to type Constructable<*Entity*> #166

Closed ludgerey closed 8 months ago

ludgerey commented 8 months ago

When trying to recreate the examples from README, I get a type error.

Factory:

export class EquipmentFactory extends Factory<EquipmentsEntity> {
  protected entity: EquipmentsEntity; // <-- here
  protected dataSource: DataSource;
  protected attrs(): FactorizedAttrs<EquipmentsEntity> {
    return {
      title: faker.vehicle.vehicle(),
      // [...]
    };
  }
}

The error:

Type EquipmentsEntity is not assignable to type Constructable<EquipmentsEntity>

Of cause I tried wrapping the Entity with Constructable, but this results in a runtime error, when used as following:

await new EquipmentFactory().create();

Results in

this.entity is not a constructor
TypeError: this.entity is not a constructor
    at EquipmentFactory.makeEntity (.../node_modules/@jorgebodega/typeorm-factory/src/factory.ts:69:20)
    at EquipmentFactory.create (.../factory.ts:43:31)
 [...]

How to solve this? Is this a bug? At least it varies from the README.

Versions:

├── @jorgebodega/typeorm-factory@1.4.0
├── @nestjs/core@10.3.3
├── @nestjs/typeorm@10.0.2
├── ts-node@10.9.2
├── typeorm@0.3.20
├── typescript@5.3.3
jorgebodega commented 8 months ago

Without a proper example with the code, I cannot do much.

Can you try to upload a simple repo with the case?

EDIT: Maybe is because you are trying to assign a type instead of value.

export class EquipmentFactory extends Factory<EquipmentsEntity> {
-       protected entity: EquipmentsEntity;
+       protected entity = EquipmentsEntity;
    ...
}
ludgerey commented 8 months ago

Sorry, I totally missed that slight difference. This solved it!