gpt4 book ai didi

typescript - 扩展@types - 从接口(interface)中删除字段,将类型添加到接口(interface)中的字段

转载 作者:搜寻专家 更新时间:2023-10-30 21:16:29 25 4
gpt4 key购买 nike

我有来自 npm/@types 的类型的 javascript 库。

我需要对@types 进行两个修复,这仅适用于我的应用程序,因此我无法将它们合并到 DefinitelyTyped 存储库中。

我需要:

  1. 从界面中删除一个字段。示例:

    // before changes:
    interface A {
    a?:string;
    b?:string;
    c?:string;
    }

    // after changes:
    interface A {
    a?:string;
    c?:string;
    }
  2. 向界面中的一个字段添加更多类型。示例:

    // before changes:
    interface B {
    a?: C;
    }

    // after changes:
    interface B {
    a?: C | D;
    }

此外,我还想从外部存储库下载主要的@types 定义。

实现此目标的最佳方法是什么?

最佳答案

这可以用下面的方法解决。

import { A as AContract, B as BContract, C, D } from './contracts.ts';

// Removes 'b' property from A interface.
interface A extends Omit<AContract, 'b'> { }

interface B extends BContract {
a?: C | D;
}

关于typescript - 扩展@types - 从接口(interface)中删除字段,将类型添加到接口(interface)中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48231422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com