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

为何在TypeORM的FindOptions中无法使用In()操作符?

解决TypeORM中使用In()操作符的TypeScript类型错误

以下是几个可行的解决方向:

  • 检查实体id字段类型
    确保你的实体类中id字段的类型与传入的数组元素类型一致。比如你传入的是字符串uuid,实体定义应该是:
import { Entity, PrimaryColumn } from "typeorm";

@Entity()
export class YourEntity {
  @PrimaryColumn()
  id: string; // 必须是string类型,不能是number或其他类型
}
  • 确认In操作符的正确导入
    从TypeORM官方包导入In,避免导入错误:
import { In } from "typeorm";
  • 直接使用数组替代In()(更简洁的写法)
    TypeORM支持直接将数组赋值给查询字段,会自动转换为IN查询,这种写法类型兼容性更好:
this.repository.find({
  where: {
    id: ['uuid-1', 'uuid-2'],
  },
  ...options,
  withDeleted: false,
});
  • 升级TypeORM版本
    部分旧版本的TypeORM在FindOperator的类型定义上存在bug,升级到最新稳定版可以解决此类问题:
npm install typeorm@latest
  • 类型断言(临时方案)
    如果以上方法都无效,可以用类型断言绕过TypeScript检查(不推荐长期使用,优先排查类型匹配问题):
this.repository.find({
  where: {
    id: In(['uuid-1', 'uuid-2']) as unknown as FindOptionsWhereProperty<NonNullable<YourEntity["id"]>>,
  },
  ...options,
  withDeleted: false,
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 00:20:32