alex4answ
@alex4answ

Как передать класс модели sequelize в качестве параметра?

TS ругается, пробовал вот разными способами:
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');
}
  • Вопрос задан
  • 43 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы