简体   繁体   中英

TypeScript interface with function

How can I create an interface which holds functions. I have tried this:

interface ILeonardo {
  addState(state: ILeonardoState),
  addStates(arr: Array<ILeonardoState>)
}

interface ILeonardoState {
  name: string,
  url: string,
  verb: string,
  options: Array<{name: string, status: number, data?: any, delay?: number}>
} 

but then I can't add the return type of addState and addStates .

Try this (replace the return value that you need)

interface ILeonardo {
   addState(state: ILeonardoState): boolean;
   addStates(arr: Array<ILeonardoState>): void;
}

The function deceleration need to end with semicolon.

http://www.typescriptlang.org/Handbook#interfaces

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM