首页 > 解决方案 > 错误 TS2322:类型“任何”不可分配给类型“从不”

问题描述

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

我的代码正在处理版本"typescript": "3.4.x", "mongoose": "4.x"

我的代码片段如下: 在此处输入图像描述

Collections如下:

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

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

标签: node.jstypescriptmongoose

解决方案


问题在于传递给 model< 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
    }]
  })
);

繁荣它开始工作。


推荐阅读