【问题标题】:Circular dependency in interface接口中的循环依赖
【发布时间】:2021-10-30 13:38:58
【问题描述】:

我有这个 interface 在我的 MongoDB 中定义一个架构,我在其中使用 mongoose 作为 ODM

import mongoose, { model, Schema, Model, Document } from "mongoose";

import { IUser } from "./user";
import { IPost } from "./posts";

export interface IComment extends Document {
  post: IPost;
  replyTo: IComment;
  owner: IUser;
  content: string;
  replies: IComment[];
  createdAt: Date;
  updatedAt: Date;
  upvotes: number;
  downvotes: number;
}

// ...mongoose schema declaration omitted

当我尝试编译 Typescript 时,我得到了这个:

Type error: Type of property 'replyTo' circularly references itself in mapped type 'LeanDocument<IComment>'.

这在我使用 mongoose@5.13.5 时从未发生过,但我必须升级到 mongoose@6.0.3 才能连接到 Atlas 上的无服务器实例。我不认为降级是一种选择。如何解决此循环依赖问题?

【问题讨论】:

    标签: typescript mongodb mongoose next.js


    【解决方案1】:

    这不应该取决于您的猫鼬版本。此错误与使用的 TypeScript 版本有关。从 TypeScript 3.7 开始允许递归类型 https://github.com/microsoft/TypeScript/pull/33050

    【讨论】:

    • 我正在使用 Typescript^4.3.5 和 ts-node^10.2.1。虽然是的,这显然是打字稿而不是 mongo 的问题..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 2010-09-12
    • 2021-10-02
    相关资源
    最近更新 更多