0

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 PluginCommands to a (preferably) defined object where the key can be any valid string?

1
  • Looking for Record<string, PluginCommand> perhaps? Equivalently: {[key: string]: PluginCommand}. Commented Dec 15, 2020 at 16:42

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

How would this work for an object of multiple elements? like {a:PluginCommand,b:PluginCommand...}?
Given the above interface "Foo", and the function "PluginCommand", you could just do: const myObject: Foo = { a: PluginCommand, b: PluginCommand }
Ah, yes, I understand now. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.