简体   繁体   English

如何将特定功能的 object 定义为类型或接口?

[英]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.我试图查看其他类似的问题,但其中大多数似乎是在定义 arrays 而不是对象。

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.作为一个整体,我对 Typescript 大多是新手,并且我仍在尝试解决一些概念,在 C# 和 JavaScript 方面都有经验。

I'm making an object that is a collection of one or more named functions in an object.我正在制作一个 object,它是 object 中一个或多个命名函数的集合。 Each function accepts an array of strings, or multiple strings, as the following definition:每个 function 都接受一个字符串数组或多个字符串,定义如下:

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?现在,如何将这些PluginCommand中的多个分配给(最好)定义的 object ,其中密钥可以是任何有效的字符串?

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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