【问题标题】:error TS2322: Type 'any' is not assignable to type 'never'错误 TS2322:类型“任何”不可分配给类型“从不”
【发布时间】:2020-05-07 14:31:13
【问题描述】:

我正在使用"typescript"- "3.8.3""mongoose": "5.9.11"

我的代码在 "typescript": "3.4.x""mongoose": "4.x" 版本上运行。

我的代码 sn-p 如下:

Collections 如下:

export let Collections = {
  identity: "identities",
  calllog: "calllog",
  calllogs: "calllogs"
};

我遇到了一些相关的错误> TypeScript/issues/31663 但不知道如何解决它。

【问题讨论】:

    标签: node.js typescript mongoose


    【解决方案1】:

    问题在于传递给模型any>和Schema的类型

    我创建的界面如下:

    import {Document, Types} from "mongoose";
    export interface CallLogsInterface extends Document {
        user: Types.ObjectId,
        logs: Types.ObjectId []
    }
    

    并将接口传递给模型:

    export const ModelCalllogs = model<CallLogsInterface>(
      Collections.calllogs,
      new Schema<CallLogsInterface>({
        user: {
          type: Schema.Types.ObjectId,
          required: true,
          ref: Collections.identity
        },
        logs: [{
          type: Schema.Types.ObjectId,
          required: true,
          ref: Collections.calllog
        }]
      })
    );
    

    Boom 它开始工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-10
      • 2019-02-07
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      相关资源
      最近更新 更多