typescript - 在没有 'Document' 的情况下从 Mongoose 模式中提取 TS 接口(interface)?

标签 typescript mongoose typescript-typings

我正在使用 ts-mongoose,因此我可以在一个地方为我的数据定义接口(interface)和模式。然后我将它们导出为 mongoose 模式 + 实际接口(interface)。

我遇到的问题是找到一种简单的 1 步方法来提取该接口(interface),而无需所有“文档”方法和属性。

我从 github 问题中将这两个编译在一起:

type ExtractDoc<T> = T extends Model<infer U> ? U : never;
type ExtractI<T> = Pick<T, Exclude<keyof T, keyof Document>>;

我一直在尝试编写一个可重用的东西来将两者结合起来:

export type ExtractInterface<T> = ExtractI<ExtractDoc<T>>;

但是当我尝试将它与架构一起使用时:

export type IExternalUser = ExtractInterface<ExternalUser>;

正在抛出:指的是一个值,但在这里被用作类型。

但如果我用额外的 2 个步骤来完成它,它就会起作用并且一切都很顺利。

有没有一种方法可以通过调用 1 次可重用类型来提取干净的接口(interface)?

最佳答案

你需要放一个<typeof><ExternalUser> 之前

export type IExternalUser = ExtractInterface<typeof ExternalUser>;

关于typescript - 在没有 'Document' 的情况下从 Mongoose 模式中提取 TS 接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56705269/

相关文章:

typescript-typings - typescript : Evaluate type of generic function

javascript - 当我点击浏览器上的“查看源代码”时,无法查看 Html

javascript - 可以在 TypeScript 中扩展 JQueryEventObject

typescript - 导出与返回值

node.js - mongoose 使用 find() 获取过去 5 分钟内创建的记录

node.js - node js mongoose从文档数组中的id中删除a

javascript - Mongoose getter正在获取未定义的参数

angular - 使用自己的类型发布 angular 包 - 类型引用包含错误的路径

typescript - 在定义中为类方法起别名

javascript - typescript 中的原型(prototype) :Element implicitly has an 'any' type because type '_BaseOption' has no index signature