[英]Extending @types - delete field from interface, add type to field in interface
I have javascript library with types from npm/@types.我有来自 npm/@types 类型的 javascript 库。
I need to make two fixes to @types which applies only in case of my application, so I can't merge them into DefinitelyTyped repository.我需要对仅适用于我的应用程序的 @types 进行两个修复,因此我无法将它们合并到 DescribeTyped 存储库中。
I need to:我需要:
remove one of fields from interface.从界面中删除字段之一。 Example:示例:
// before changes: interface A { a?:string; b?:string; c?:string; } // after changes: interface A { a?:string; c?:string; }
add more types to one field in interface.为界面中的一个字段添加更多类型。 Example:示例:
// before changes: interface B { a?: C; } // after changes: interface B { a?: C | D; }
Also I still want to download main @types definitions from external repository.此外,我仍然想从外部存储库下载主要的 @types 定义。
What is the best way to achieve this?实现这一目标的最佳方法是什么?
This can be solved using the following method.这可以使用以下方法解决。
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;
}
You cannot override type declarations of existing properties of interfaces in TypeScript but you could do this by extending the type interfaces since you can override property types:您不能覆盖 TypeScript 中接口的现有属性的类型声明,但您可以通过扩展类型接口来实现,因为您可以覆盖属性类型:
interface afterA extends A {
b?: never;
}
interface afterB extends B {
a?: C | D;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.