简体   繁体   中英

How to define an object of specific functions as a type or an interface?

I've attempted to look at other similar questions , but most of them appear to be defining arrays instead of objects.

I'm mostly new to Typescript as a whole, and am still trying to wrangle some of the concepts, having both experience in C# and JavaScript.

I'm making an object that is a collection of one or more named functions in an object. Each function accepts an array of strings, or multiple strings, as the following definition:

define function PluginCommand(argv: array, ...args:string[]): number;

Now, how can I assign multiple of these PluginCommand s to a (preferably) defined object where the key can be any valid string?

You should be able to do something like:

interface Foo {
  bar: (args: Array<string>) => number;
}

If you want the key to be any string, do:

interface Foo {
  [key: string]: (args: Array<string>) => number;
}

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