假設您有三種型別的物件,例如:
interface A {
testA: someDifferentType;
}
interface B {
testB: someOtherType[];
}
interface C {
testC1: string;
testC2: number;
}
現在我有一個根物件,它是一個可能包含這 3 個介面的物件(但沒有一個是必需的)所以例如這是一個有效的物件:
{
root: {
testB: [{...}],
testC1: 'test',
testC2: 123
}
}
它應該是什么樣的型別或介面?
uj5u.com熱心網友回復:
然后您只需要定義物件,使其成為部分介面的聯合:
interface MyObj {
root: Partial<A> & Partial<B> & Partial<C>
}
const myObj: MyObj = {
root: {
testB: [{...}],
testC1: 'test',
testC2: 123
}
};
您還可以將它們組合成一個部分型別,即:
interface MyObj {
root: Partial<A & B & C>
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/407780.html
標籤:
下一篇:強制泛型型別的交集以擴展給定型別
