我解释我有这个属性定义
export interface CrudOpts {
Model?: ModelCtor<Model>;
only?: string[];
except?: string[];
nestedRelations?: boolean;
basepath?: string;
}
当我在装饰中使用它时:
export function crud(opts: CrudOpts): Function {
return (target: any) => {
Reflect.defineMetadata('crud', opts, target.prototype);
}
}
然后这里:
@crud({Model: Runner})
export class RunnerController {
}
我得到这个错误
src/controllers/runner.controller.ts(6,8): error TS2322: Type 'typeof Runner' is not assignable to type 'ModelCtor<Model<any, any>>'.
Type 'typeof Runner' is not assignable to type 'typeof Model'.
Types of construct signatures are incompatible.
Type 'new (values?: RunnerCreateAttributes, options?: BuildOptions) => Runner' is not assignable to type 'abstract new <TModelAttributes extends {} = any, TCreationAttributes extends {} = TModelAttributes>(values?: TCreationAttributes, options?: BuildOptions) => Model<TModelAttributes, TCreationAttributes>'.
Types of parameters 'values' and 'values' are incompatible.
Type 'TCreationAttributes' is not assignable to type 'RunnerCreateAttributes'.
Type '{}' is not assignable to type 'Optional<RunnerAttributes, "id">'.
Type '{}' is not assignable to type 'Omit<RunnerAttributes, "id">'.
Type 'TCreationAttributes' is not assignable to type 'Omit<RunnerAttributes, "id">'.
Type '{}' is missing the following properties from type 'Omit<RunnerAttributes, "id">': email, avatar_url, firstname, lastname, and 14 more.
我的理解是,由于它们的类型定义,无法将runner模型类型分配给modelctor,但我的问题是,如何实现这种使用类型进行干净编码的功能?我们是否有一个类型来分配我们定义的模型?
以下是我的模型定义:
class Runner extends Model<RunnerAttributes, RunnerCreateAttributes> {
public id!: number;
public email!: string;
public firstname!: string;
public lastname!: string;
public avatar_url!: string;
public timeto_id!: string;
public profile_type!: string;
public gender!: string;
public is_trial_registered!: boolean;
public is_anonyme!: boolean;
public birthdate!: string;
public bib!: number;
public sas!: string;
public subscription_date!: string;
public year_id!: number;
public group_id!: number;
public subgroup_id!: number;
public subgroupcompany_id!: number;
public roles!: string[]
}
Runner.init({
id: {
type: BIGINT,
autoIncrement: true,
primaryKey: true,
allowNull: false
},
avatar_url: TEXT,
email: {
type: STRING,
allowNull: false,
},
firstname: STRING,
lastname: STRING,
timeto_id: {
type: STRING,
allowNull: false
},
profile_type: {
type: STRING,
allowNull: true,
defaultValue: 'public'
},
gender: STRING,
is_trial_registered: BOOLEAN,
is_anonyme: BOOLEAN,
birthdate: DATE,
bib: INTEGER,
sas: STRING,
subscription_date: DATE,
year_id: {
type: INTEGER,
allowNull: false
},
group_id: INTEGER,
subgroup_id: INTEGER,
subgroupcompany_id: INTEGER,
roles: {
type: ARRAY(STRING)
}
}, {
timestamps: false,
tableName: 'runner',
sequelize
})
export {Runner};
提前谢谢
暂无答案!
目前还没有任何答案,快来回答吧!