简体   繁体   中英

Dynamically create class definition including decorator with TypeScript

Is it possible to generate class definition with decorators dynamically like following?
(The code does not actually work, but shows what I would like to achieve)
*This code contains TypeORM's decorator, but the essense of the question does not involve the library knowledge.

const classGenerator = (jsType: string, dbType: string, nullable: boolean) => {
  return class {
    @Column({ type: dbType, nullable })
    val: jsType;
  };
};

Yeah, but everything you use as types must be provided as a generic parameter:

const classGenerator = <jsType extends string>(dbType: string, nullable: boolean) => {
  return class {
    @Column({ type: dbType, nullable })
    val: jsType;
  };
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM