如何使用 typescript 實作以下功能?
interface ActionType {
// how code?
type: string
};
let actionType: ActionType<{list: any}> = {
type: 'type',
list: []
}
uj5u.com熱心網友回復:
聽起來你在問一個通用的交叉點型別
// Generic type
// | Intersection type
// ↓ ↓
type ActionType<T> = T & { type: string };
const actionType: ActionType<{list: any[] }> = {
type: 'type',
list: []
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/457323.html
標籤:javascript 打字稿
下一篇:我該如何解決:“未捕獲的TypeError:無法讀取null的屬性(正在讀取'addEventListener')”?
