You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Sequelize 6.19.1版本where参数类型不兼容问题求助

Sequelize 6.19.1中where参数TypeScript类型不兼容问题解决

升级到Sequelize 6.19.1版本后,执行以下代码时出现TypeScript类型错误:

async findByUuid(uuid: string, options?: Omit<FindOptions<T>, 'where'>): Promise<T | null> {
  return this.model.findOne<T>({ where: { uuid }, ...options });
}

报错信息如下:

No overload matches this call.
Overload 1 of 2, '(this: ModelStatic, options: NonNullFindOptions): Promise', gave the following error.
Type '{ uuid: string; }' is not assignable to type 'WhereOptions'.
Types of property 'uuid' are incompatible.
Type 'string' is not assignable to type 'WhereAttributeHashValue<Attributes[string]>'.
Overload 2 of 2, '(this: ModelStatic, options?: FindOptions): Promise', gave the following error.
Type '{ uuid: string; }' is not assignable to type 'WhereOptions'.
Types of property 'uuid' are incompatible.
Type 'string' is not assignable to type 'WhereAttributeHashValue<Attributes[string]>'.ts(2769)
model.d.ts(52, 3): The expected type comes from property 'where' which is declared here on type 'NonNullFindOptions'
model.d.ts(52, 3): The expected type comes from property 'where' which is declared here on type 'FindOptions'

解决方案

1. 修正模型属性类型定义

确保你的Sequelize模型中,uuid字段的类型被明确声明为string,避免类型推导歧义:

import { Model, DataTypes } from 'sequelize';

class TargetModel extends Model {
  public uuid!: string;
  // 其他属性定义...
}

TargetModel.init({
  uuid: {
    type: DataTypes.STRING,
    allowNull: false,
    unique: true
  },
  // 其他字段配置...
}, { sequelize });

2. 类型断言临时绕过

如果模型定义正确但类型推导仍异常,可通过类型断言强制匹配类型:

import { WhereOptions } from 'sequelize';

async findByUuid(uuid: string, options?: Omit<FindOptions<T>, 'where'>): Promise<T | null> {
  return this.model.findOne<T>({ 
    where: { uuid } as WhereOptions<T['_attributes']>, 
    ...options 
  });
}

3. 升级Sequelize版本

6.19.x版本的TypeScript类型定义存在已知bug,后续稳定版本已修复,直接升级到最新6.x版本即可:

npm install sequelize@latest
# 或使用yarn
yarn add sequelize@latest

4. 调整泛型参数约束

确保方法的泛型参数与模型属性类型正确绑定,替换模糊的泛型T为具体的模型属性类型:

async findByUuid<Attrs>(uuid: string, options?: Omit<FindOptions<Attrs>, 'where'>): Promise<Model<Attrs> | null> {
  return this.model.findOne({ where: { uuid }, ...options });
}

内容的提问来源于stack exchange,提问作者Marko Nikolic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 01:48:19