How to pass Model sequelize as parameter?

1.7k views Asked by At

i try to pass a model class as parameter, but typescript doesn't see methods, properties.

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.
    // ...
  };
};

I have tried some variants:

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>'

How it will be use (something like this):

const isExistsName = isExistsValidator(UserModel, 'name');
// ...
if(await isExistsName('alex4answer')) {
  throw new Error('Name already in use');
}

What am i doing wrong? i'm new in typescript

0

There are 0 answers