V
V
Vladimir Golub2021-01-26 12:36:36
typescript
Vladimir Golub, 2021-01-26 12:36:36

How to return a class from a function?

The function substitutes values ​​into the class, but there are problems with the return.
Error: Return type of exported function has or is using private name 'EntityClass'.

import {Entity, PrimaryColumn, Column} from "typeorm";

export function createEntity(tableName: string) {
  @Entity({name: tableName})
  class EntityClass {
    public static tableName = tableName;

    @PrimaryColumn()
    public name: string = "";

    @Column()
    public value: number = 0;
  }

  return EntityClass;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daniil Vasilyev, 2021-01-26
@hello_my_name_is_dany

Take the class out of the function and export it too

@Entity({name: tableName})
export  class EntityClass {
    public static tableName = tableName;

    @PrimaryColumn()
    public name: string = "";

    @Column()
    public value: number = 0;
}

export function createEntity(tableName: string) {
  return EntityClass;
}

And in general, you should not dynamically create an entity, use inheritance

V
Vasily Bannikov, 2021-01-26
@vabka

https://medium.com/@terence410/working-with-dynami...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question