[英]How to omit one property from interface, but not with type in TypeScript?
[英]Typescript: Omit property from a generic interface
我正在嘗試創建一個省略給定類型的屬性的接口。 為此,我使用了Omit
導致 Type 所以它的定義是錯誤的。 但是,如果它不是通用接口,它就可以完美運行。
考慮以下示例。
interface IBaseType {
prop1: number;
prop2: string;
match: boolean;
}
interface OmitMatchNoGeneric extends Omit<IBaseType, "match"> {}
interface OmitMatch<T extends { match: any }> extends Omit<T, "match"> {}
function test(genericArg: OmitMatch<IBaseType>, nonGenericArg: OmitMatchNoGeneric) {
nonGenericArg.prop1 = 5; // the properties are suggested
genericArg.prop1 = 5; // intelliSense fails to list the properties
}
在這個例子中,VSCode 的智能感知顯示了非泛型參數的屬性列表,但它沒有為泛型參數顯示。 泛型參數被視為 any 類型的對象。
我主要擔心的是,如果我不應該使用Omit
我還能使用什么? 如果我想用類型而不是接口來實現它,我該怎么做?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.