![](/img/trans.png)
[英]Param type resolution when combining union and intersection types in TypeScript
[英]Combining union and intersection types with brackets
如果我有一個界面:
declare interface APIResponse {
type: string
}
還有一些有效載荷:
declare interface Payload {
prop: string
}
我還希望API響應為可為空,因此我想這樣做:
methodName: () => (APIResponse & Payload) | null
例如,將響應與交叉點類型結合在一起,但也可以使整個內容為空。 這可能嗎?
是的,它的行為完全符合您的預期:
interface APIResponse {
type: string
}
interface Payload {
prop: string
}
class Example {
methodName(): (APIResponse & Payload) | null {
// OK
return { type: 'str', prop: 'str' };
// OK
return null;
// NO! Missing 'type'
return { prop: 'str' };
// NO! Missing 'prop'
return { type: 'str' };
}
}
我在其中留下了多個退貨陳述作為示例-顯然您只能有一個:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.