From c4091e488889ecd59e09c520c07c4d87752182cf Mon Sep 17 00:00:00 2001 From: Diogo Franco Date: Tue, 4 Jul 2017 11:28:17 +0900 Subject: [PATCH 1/5] Improve type of HandlebarsTemplateDelegate Adds an optional type argument that should be useful for .hbs.d.ts files. The RuntimeOptions were specified by looking at https://github.com/wycats/handlebars.js/blob/680ec96/lib/handlebars/runtime.js#L131-L171 -- these don't seem to be documented anywhere. --- types/handlebars/index.d.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/types/handlebars/index.d.ts b/types/handlebars/index.d.ts index 71cc0a2cfe..7572d19824 100644 --- a/types/handlebars/index.d.ts +++ b/types/handlebars/index.d.ts @@ -96,8 +96,8 @@ interface HandlebarsTemplatable { template: HandlebarsTemplateDelegate; } -interface HandlebarsTemplateDelegate { - (context: any, options?: any): string; +interface HandlebarsTemplateDelegate { + (context: T, options?: RuntimeOptions): string; } interface HandlebarsTemplates { @@ -108,6 +108,14 @@ interface TemplateSpecification { } +interface RuntimeOptions { + partial?: boolean; + depths?: any[]; + helpers?: { [name: string]: Function } + partials?: { [name: string]: HandlebarsTemplateDelegate } + decorators?: { [name: string]: Function } +} + interface CompileOptions { data?: boolean; compat?: boolean; From 6af5d2a55f506eeb2304904371e465f05b6ba5fe Mon Sep 17 00:00:00 2001 From: Diogo Franco Date: Tue, 4 Jul 2017 11:35:54 +0900 Subject: [PATCH 2/5] Update TypeScript version --- types/handlebars/index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/types/handlebars/index.d.ts b/types/handlebars/index.d.ts index 7572d19824..f4cba279bf 100644 --- a/types/handlebars/index.d.ts +++ b/types/handlebars/index.d.ts @@ -2,6 +2,7 @@ // Project: http://handlebarsjs.com/ // Definitions by: Boris Yankov // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 declare namespace Handlebars { From 7259fc89f93797f084f019725c808549f95d92f9 Mon Sep 17 00:00:00 2001 From: "Diogo Franco (Kovensky)" Date: Tue, 4 Jul 2017 11:43:41 +0900 Subject: [PATCH 3/5] Update the minimum required version on affected files --- types/handlebars/index.d.ts | 1 - types/koa-hbs/index.d.ts | 3 ++- types/swag/index.d.ts | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/types/handlebars/index.d.ts b/types/handlebars/index.d.ts index f4cba279bf..17014d464a 100644 --- a/types/handlebars/index.d.ts +++ b/types/handlebars/index.d.ts @@ -4,7 +4,6 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 - declare namespace Handlebars { export function registerHelper(name: string, fn: Function, inverse?: boolean): void; export function registerHelper(name: Object): void; diff --git a/types/koa-hbs/index.d.ts b/types/koa-hbs/index.d.ts index 481738efc3..cace80c852 100644 --- a/types/koa-hbs/index.d.ts +++ b/types/koa-hbs/index.d.ts @@ -2,6 +2,7 @@ // Project: https://github.com/gilt/koa-hbs // Definitions by: Jacob Malone , Mudkip // Definitions: https://github.com/jcbmln/DefinitelyTyped +// TypeScript Version: 2.3 /* =================== USAGE =================== @@ -57,4 +58,4 @@ declare module "koa" { export interface Context { render(tpl: string, locals?: {[key: string]: any}): Promise; } -} \ No newline at end of file +} diff --git a/types/swag/index.d.ts b/types/swag/index.d.ts index 29ba129fe5..cd7bb6ccaa 100644 --- a/types/swag/index.d.ts +++ b/types/swag/index.d.ts @@ -2,6 +2,7 @@ // Project: https://github.com/elving/swag // Definitions by: Shogo Iwano // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 /// From fda59743791dd3c720ec557eefc47a0109b89c39 Mon Sep 17 00:00:00 2001 From: "Diogo Franco (Kovensky)" Date: Tue, 4 Jul 2017 11:47:10 +0900 Subject: [PATCH 4/5] Use the typed HandlebarsTemplateDelegate in tests --- types/handlebars/handlebars-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/handlebars/handlebars-tests.ts b/types/handlebars/handlebars-tests.ts index 71215b5294..f5105c2122 100644 --- a/types/handlebars/handlebars-tests.ts +++ b/types/handlebars/handlebars-tests.ts @@ -36,7 +36,7 @@ var post = { url: '/hello-world', body: 'Hello World!' }; var context2 = { posts: [post] }; var source2 = '
    {{#posts}}
  • {{{link_to this}}}
  • {{/posts}}
'; -var template2 = Handlebars.compile(source2); +var template2: HandlebarsTemplateDelegate<{ posts: { url: string, body: string }[] }> = Handlebars.compile(source2); template2(context2); Handlebars.registerHelper('link_to', (title: string, context: typeof post) => { From 729a3d6985090700675ed46549496d4b5c749d4a Mon Sep 17 00:00:00 2001 From: "Diogo Franco (Kovensky)" Date: Tue, 4 Jul 2017 11:48:48 +0900 Subject: [PATCH 5/5] Also add template arguments to Handlebars.compile and Handlebars.template --- types/handlebars/handlebars-tests.ts | 4 ++-- types/handlebars/index.d.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/types/handlebars/handlebars-tests.ts b/types/handlebars/handlebars-tests.ts index f5105c2122..95342c2e63 100644 --- a/types/handlebars/handlebars-tests.ts +++ b/types/handlebars/handlebars-tests.ts @@ -45,14 +45,14 @@ Handlebars.registerHelper('link_to', (title: string, context: typeof post) => { var context3 = { posts: [{url: '/hello-world', body: 'Hello World!'}] }; var source3 = '
    {{#posts}}
  • {{{link_to "Post" this}}}
  • {{/posts}}
'; -var template3 = Handlebars.compile(source3); +var template3 = Handlebars.compile(source3); template3(context3); var source4 = '
    {{#people}}
  • {{#link}}{{name}}{{/link}}
  • {{/people}}
'; Handlebars.registerHelper('link', function(context: any) { return '' + context.fn(this) + ''; }); -var template4 = Handlebars.compile(source4); +var template4 = Handlebars.compile<{ people: { name: string, id: number }[] }>(source4); var data2 = { 'people': [ { 'name': 'Alan', 'id': 1 }, { 'name': 'Yehuda', 'id': 2 } diff --git a/types/handlebars/index.d.ts b/types/handlebars/index.d.ts index 17014d464a..56c341aa47 100644 --- a/types/handlebars/index.d.ts +++ b/types/handlebars/index.d.ts @@ -19,9 +19,9 @@ declare namespace Handlebars { export function Exception(message: string): void; export function log(level: number, obj: any): void; export function parse(input: string): hbs.AST.Program; - export function compile(input: any, options?: CompileOptions): HandlebarsTemplateDelegate; + export function compile(input: any, options?: CompileOptions): HandlebarsTemplateDelegate; export function precompile(input: any, options?: PrecompileOptions): TemplateSpecification; - export function template(precompilation: TemplateSpecification): HandlebarsTemplateDelegate; + export function template(precompilation: TemplateSpecification): HandlebarsTemplateDelegate; export function create(): typeof Handlebars;