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

使用Sequelize的Op.between时遭遇TypeScript类型错误求助

问题:Sequelize中Op操作符无法正常使用,TypeScript类型报错

我确认自己的操作存在问题,但始终找不到具体原因,恳请各位提供相关提示。

我的服务函数中这段代码执行失败(Visual Studio Code会标记where子句):

const getExtractionModels = async (repoId: number, filterFrom: Date, filterTo: Date): Promise<ExtractionModel[]> => {
    return await ExtractionModel.findAll({
        where: { 
            // repositoryRef: repoId,
            startDate: {
                [Op.between]: [filterFrom, filterTo]
            }
        }
    })
}

我在Stack Overflow的帖子中看到需要将日期转成字符串(通过.toISOString()转换)传入Op.between,但这并没有解决问题。

模型类定义如下:

@Table({ tableName: 'extraction' })
export class ExtractionModel extends Model<InferAttributes<ExtractionModel>, InferCreationAttributes<ExtractionModel>> {

    ...

    @Attribute(DataTypes.DATE)
    @Default(DataTypes.NOW)
    declare startDate: CreationOptional<Date>

    @Attribute(DataTypes.STRING)
    @NotNull
    declare branches: string

    ...
}

TypeScript给出的错误信息如下:

No overload matches this call.
  Overload 1 of 2, '(this: ModelStatic<ExtractionModel>, options: Omit<FindOptions<InferAttributes<ExtractionModel, { omit: never; }>>, "raw"> & { raw: true; }): Promise<...>', gave the following error.
    Type '{ startDate: { [OpTypes.between]: Date[]; }; }' is not assignable to type 'WhereOptions<InferAttributes<ExtractionModel, { omit: never; }>>'.
      Types of property 'startDate' are incompatible.
    Type '{ [OpTypes.between]: Date[]; }' is not assignable to type 'WhereAttributeHashValue<Date & { [CreationAttributeBrand]?: true | undefined; }>'.
      Types of property '[Op.between]' are incompatible.
        Type 'Date[]' is not assignable to type 'undefined'.
  Overload 2 of 2, '(this: ModelStatic<ExtractionModel>, options?: FindOptions<InferAttributes<ExtractionModel, { omit: never; }>> | undefined): Promise<...>', gave the following error.
    Type '{ startDate: { [OpTypes.between]: Date[]; }; }' is not assignable to type 'WhereOptions<InferAttributes<ExtractionModel, { omit: never; }>>'.
      Types of property 'startDate' are incompatible.
    Type '{ [OpTypes.between]: Date[]; }' is not assignable to type 'WhereAttributeHashValue<Date & { [CreationAttributeBrand]?: true | undefined; }>'.
      Types of property '[Op.between]' are incompatible.
        Type 'Date[]' is not assignable to type 'undefined'.ts(2769)
model.d.ts(96, 3): The expected type comes from property 'where' which is declared here on type 'Omit<FindOptions<InferAttributes<ExtractionModel, { omit: never; }>>, "raw"> & { raw: true; }'

我尝试了其他字段,错误依然相同,例如:

return await ExtractionModel.findAll({
    where: { 
        // repositoryRef: repoId,
        branches: {
            [Op.eq]: '1'
        }
    }
})

请问为何Op操作符无法正常工作?这是否与我在模型中使用装饰器有关?(希望不是)

依赖版本:

"@sequelize/core": "^7.0.0-alpha.37",
"sequelize": "^6.37.3",

更新

使用[Op.between]: [filterFrom, filterTo]时的错误:

Type '{ repositoryRef: number; status: string; startDate: { [OpTypes.between]: Date[]; }; }' is not assignable to type 'WhereOptions<InferAttributes<ExtractionModel, { omit: never; }>>'.
  Types of property 'startDate' are incompatible.
    Type '{ [OpTypes.between]: Date[]; }' is not assignable to type 'WhereAttributeHashValue<Date & { [CreationAttributeBrand]?: true | undefined; }>'.
      Types of property '[Op.between]' are incompatible.

使用[Op.eq]: '1'时的错误:

Type '{ branches: { [OpTypes.eq]: string; }; }' is not assignable to type 'WhereOptions<InferAttributes<ExtractionModel, { omit: never; }>>'.
  Types of property 'branches' are incompatible.
    Type '{ [OpTypes.eq]: string; }' is not assignable to type 'WhereAttributeHashValue<string>'.
      Types of property '[Op.eq]' are incompatible.

目前我只能在Sequelize中执行相等查询,希望该错误与模型中使用的装饰器无关,它们应该能和Op正常配合,不是吗?


解决方案

1. 核心问题:依赖版本冲突

你同时安装了@sequelize/core@7.x(alpha测试版)和sequelize@6.x稳定版,这两个版本的类型定义完全不兼容。Sequelize 7将核心模块拆分到@sequelize/core,而6.x的类型系统与7.x的Op操作符、WhereOptions定义无法匹配,直接导致TypeScript类型校验失败。

修复方案二选一:

  • 完全迁移到Sequelize 7:卸载sequelize包,仅保留@sequelize/core,同时确保数据库驱动、装饰器相关依赖都适配7.x版本(注意7.x是alpha版,可能存在其他未修复的问题)。
  • 回退到Sequelize 6.x:卸载@sequelize/core,仅使用sequelize@6.37.3,6.x版本的装饰器需要依赖sequelize-typescript包,需单独安装并从该包导入装饰器。

2. 6.x版本装饰器的正确使用方式

如果选择回退到Sequelize 6,需安装sequelize-typescript,并从该包导入装饰器:

import { Table, Column, Model, DataType, NotNull, Default } from 'sequelize-typescript';

@Table({ tableName: 'extraction' })
export class ExtractionModel extends Model<ExtractionModel> {
  @Column({ type: DataType.DATE, defaultValue: DataType.NOW })
  startDate: Date;

  @Column({ type: DataType.STRING, allowNull: false })
  branches: string;
}

3. Op操作符的正确导入

  • Sequelize 6:import { Op } from 'sequelize';
  • Sequelize 7:import { Op } from '@sequelize/core';

4. 临时绕过方案(不推荐)

如果暂时无法调整版本,可以用类型断言跳过TypeScript检查,但这只是权宜之计:

return await ExtractionModel.findAll({
  where: {
    startDate: {
      [Op.between]: [filterFrom, filterTo]
    }
  } as any
})

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 11:18:09