Home > Back-end >  How typescript creates generic interfaces
How typescript creates generic interfaces

Time:04-07

How to use typescript to realize the following functions?

interface ActionType {
  // how code?
  type: string
};

let actionType: ActionType<{list: any}> = {
  type: 'type',
  list: []
}

CodePudding user response:

Sounds like you're asking about a generic intersection type

//              Generic type
//              |      Intersection type
//              ↓      ↓
type ActionType<T> = T & { type: string };

const actionType: ActionType<{list: any[] }> = {
  type: 'type',
  list: []
};
  • Related