Home > Back-end >  Typescript dictionary of Promises initialized without keys
Typescript dictionary of Promises initialized without keys

Time:08-26

I am relatively new to Typescript/Javascript and came across the following snippet in a VSCode extension sample:

const options: { [key: string]: (context: ExtensionContext) => Promise<void> } = {
    showQuickPick,
    showInputBox,
    multiStepInput,
    quickOpen,
};

I tried something similar with primitive types but that generates an error about missing ':'.

The developer console shows that the function names are being used as keys. How/why does the code in the example above work?

CodePudding user response:

These are called shorthand properties, it's a feature available since ES6/ES2015

const a = 'foo';
const b = 42;
const c = {};
o = { a: a, b: b, c: c };


// Shorthand property names
o = { a, b, c };
  • Related