Skip to content

Typescript 3.7+ useDefineForClassFields flag may break Vendure BaseEntity initialization  #2099

@tianyingchun

Description

@tianyingchun

Describe the bug
tsconfig.json

  "compilerOptions": {
       "useDefineForClassFields": true,
  }

vendure/base.entity.ts

export abstract class VendureEntity {
    protected constructor(input?: DeepPartial<VendureEntity>) {
        if (input) {
            for (const [key, value] of Object.entries(input)) {
                (this as any)[key] = value;
            }
        }
    }

defined new entity class named Channel.entity.ts

export class ModuleChannel extends VendureEntity {
  constructor(input?: DeepPartial<ModuleChannel>) {
    super(input);
  }

  @Column({ unique: true })
  code: string;

  @Column({ unique: true })
  token: string;

in your some xxx.service.ts

you may need to use

 defaultChannel = new ModuleChannel({
        code: "1111",
        token:"token",
      });

above code defaultChannel.code ,token will always equal undefined

please see.
https://www.typescriptlang.org/tsconfig#useDefineForClassFields
https://swc.rs/docs/migrating-from-tsc#usedefineforclassfields
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#the-usedefineforclassfields-flag-and-the-declare-property-modifier
https://www.typescriptlang.org/docs/handbook/2/classes.html

When target >= ES2022 or useDefineForClassFields is true, class fields are initialized after the parent class constructor completes, overwriting any value set by the parent class. This can be a problem when you only want to re-declare a more accurate type for an inherited field. To handle these cases, you can write declare to indicate to TypeScript that there should be no runtime effect for this field declaration.

Expected behavior
Maybe we should refactor base.entity.ts to avoid configuration side effects of useDefineForClassFields

Environment (please complete the following information):

  • @vendure/core version: 1.9.4
  • Nodejs version 16+
  • Database (mysql/postgres etc): mysql

Additional context
Add any other context about the problem here.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions