【问题标题】:Typescript - Type an Interface?打字稿 - 键入接口?
【发布时间】:2021-02-02 14:46:15
【问题描述】:

如果这有点含糊,我深表歉意,因为我不是 100% 使用打字稿。有没有办法在界面中添加打字?

技术:Angular 9、Swagger 和 TypeScript。

这是错误的,但是是这样的:

export interface PasswordRequest: components["schemas"]["PasswordRequest"] {}

我不是在寻找:

export interface PasswordRequest {
  email?: string | null;
  oldPassword?: string | null;
}

说来话长,但基本上 swagger 正在被改造到旧应用程序中。吐出来了

    export interface components { schemas: { PasswordRequest: { email?: string | null; oldPassword?: string | null; newPassword?: string | null; };
with all the needed interface under this components > schemas structure.

我试图避免翻录/重命名接口和所有导入。

【问题讨论】:

  • “键入界面”是什么意思?接口本身一种可以应用于事物的类型。
  • 我不清楚你的目标是什么。你想从components["schemas"]["PasswordRequest"] 找到的类型开始,然后调整它以创建一个新类型吗?
  • @NicholasTower 说来话长,但基本上大摇大摆的东西正在被复古装进旧应用程序。它吐出'导出接口组件{模式:{密码请求:{电子邮件?:字符串|空值; oldPassword?: 字符串 |空值;新密码?:字符串 |空值; };'我试图避免翻录/重命名接口和所有导入。
  • 如果您只是希望能够通过名称PasswordRequest 引用它,那么您可以使用export type PasswordRequest = components["schemas"]["PasswordRequest"]。如果这不是你的意思,那么我仍然需要更多关于你的目标的细节
  • 看看打字稿中的高级类型typescriptlang.org/docs/handbook/…

标签: angular typescript swagger typescript-typings


【解决方案1】:

如果您使用类型来提取界面的相关部分,它应该可以工作

export type PasswordRequest  = components['schemas']['PasswordRequest']

【讨论】:

    猜你喜欢
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 2014-08-07
    • 2018-12-29
    • 1970-01-01
    • 2020-08-14
    • 2019-05-04
    相关资源
    最近更新 更多