TypeScript Function Hash Table Definition

I am trying to create a definition file for Handlebars for use with precompiled steering scripts. Handlebars places the precompiled scripts in a string indexed hash of the functions, but I cannot figure out how this will be determined.

Hypothetical definition:

declare module Handlebars { export var templates: { (model:any) => string; }[index: string]; } 

but this is not a valid definition. The definition should work for a call as follows:

 var myHtml = Handlebars.templates["person-template"]({FNmae: "Eric"}); 

Such a definition is close:

 export var templates: { (model:any) => string; }[]; 

But it is an array with a numerical index, and it is not the same, and VS Intellisense simply decides what functions are in the array.

+8
typescript
source share
1 answer

What you want to use is an object type with an index signature (see section 3.5.3, in particular 3.5.3.3).

 declare module Handlebars { export var templates: { [s: string]: (model: any) => string; } } 
+12
source share

All Articles