node.js - วิธีส่ง Model ของ sequelize เป็นพารามิเตอร์?
ฉันพยายามส่งคลาสโมเดลเป็นพารามิเตอร์ แต่ TypeScript ไม่เห็นเมธอดและคุณสมบัติ
import type { Model } from 'sequelize';
const isExistsValidator = (model: typeof Model, field: string) => {
return async (value): boolean => {
const result = await model.findOne({ where: { [field]: value } }); // No overload matches this call.
// ...
};
};
ฉันได้ลองใช้ตัวแปรบางอย่างแล้ว:
import type { Model, ModelStatic } from 'sequelize';
const isExistsValidator = <M extends Model>(model: ModelStatic<M>, field: string) {
// ...
const result = await model.findOne({ where: { [field]: value } });// Property 'findOne' does not exist on type
}
import type { Model } from 'sequelize';
const isExistsValidator = (model: Model, field: string) {
// ...
const result = await model.findOne({ where: { [field]: value } });// Property 'findOne' is a static member of type 'Model<any, any>'
จะใช้มันอย่างไร (ประมาณแบบนี้):
const isExistsName = isExistsValidator(UserModel, 'name');
// ...
if(await isExistsName('alex4answer')) {
throw new Error('Name already in use');
}
ฉันทำอะไรผิดหรือเปล่า? ฉันเพิ่งเริ่มใช้ไทป์สคริปต์