【问题标题】:Sequelize count return value is not assignable to type 'number'续集计数返回值不可分配给类型“数字”
【发布时间】:2020-03-07 17:49:03
【问题描述】:

我正在将使用 TypeScript 编写的 Express 应用程序的依赖项从 sequelize 第 4 版更新到第 5 版。第 5 版已内置类型支持,并且该项目还使用了 sequelize-typescript 包。

在模型上调用.count() 方法的任何地方都发生了一个构建错误,我无法完全弄清楚如何从文档中修复:

const quantity: number = await MyModel.count();

输入 '{ [key: string]: number; }' 不可分配给类型 'number'。

似乎 count 应该简单地返回一个 number 类型,但我不确定为什么有 [key: string]: 或如何使用它......

【问题讨论】:

    标签: typescript sequelize.js


    【解决方案1】:

    如果查询包含group选项,则返回值不同:

    sequelize/types/lib/model.d.ts

      /**
       * Count number of records if group by is used
       */
      public static count(options: CountWithOptions): Promise<{ [key: string]: number }>;
    
      /**
       * Count the number of records matching the provided where clause.
       *
       * If you provide an `include` option, the number of matching associations will be counted instead.
       */
      public static count(options?: CountOptions): Promise<number>;
    

    【讨论】:

      猜你喜欢
      • 2018-02-24
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      • 1970-01-01
      • 2019-06-24
      • 2021-06-14
      相关资源
      最近更新 更多