【问题标题】:Extracting TS interface from mongoose schema without the 'Document'?从没有“文档”的猫鼬模式中提取 TS 接口?
【发布时间】:2019-11-04 09:54:40
【问题描述】:

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

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

我从一个 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>;

正在抛出:refers to a value, but is being used as a type here.

但是如果我用额外的 2 个步骤来做,它会起作用,而且一切都很好。

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

【问题讨论】:

    标签: typescript mongoose typescript-typings


    【解决方案1】:

    您需要在&lt;ExternalUser&gt; 之前添加一个&lt;typeof&gt;

    export type IExternalUser = ExtractInterface&lt;typeof ExternalUser&gt;;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-23
      • 2020-08-01
      • 2013-07-15
      • 2021-06-04
      • 2021-12-31
      • 1970-01-01
      • 2019-12-20
      • 2014-01-29
      相关资源
      最近更新 更多