Merge pull request #17726 from Kovensky/patch-2

Improve type of HandlebarsTemplateDelegate
This commit is contained in:
Ryan Cavanaugh
2017-07-12 12:10:24 -07:00
committed by GitHub
4 changed files with 19 additions and 9 deletions
+3 -3
View File
@@ -36,7 +36,7 @@ var post = { url: '/hello-world', body: 'Hello World!' };
var context2 = { posts: [post] };
var source2 = '<ul>{{#posts}}<li>{{{link_to this}}}</li>{{/posts}}</ul>';
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) => {
@@ -45,14 +45,14 @@ Handlebars.registerHelper('link_to', (title: string, context: typeof post) => {
var context3 = { posts: [{url: '/hello-world', body: 'Hello World!'}] };
var source3 = '<ul>{{#posts}}<li>{{{link_to "Post" this}}}</li>{{/posts}}</ul>';
var template3 = Handlebars.compile(source3);
var template3 = Handlebars.compile<typeof context3>(source3);
template3(context3);
var source4 = '<ul>{{#people}}<li>{{#link}}{{name}}{{/link}}</li>{{/people}}</ul>';
Handlebars.registerHelper('link', function(context: any) {
return '<a href="/people/' + this.id + '">' + context.fn(this) + '</a>';
});
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 }
+13 -5
View File
@@ -2,7 +2,7 @@
// Project: http://handlebarsjs.com/
// Definitions by: Boris Yankov <https://github.com/borisyankov/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
declare namespace Handlebars {
export function registerHelper(name: string, fn: Function, inverse?: boolean): void;
@@ -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<T = any>(input: any, options?: CompileOptions): HandlebarsTemplateDelegate<T>;
export function precompile(input: any, options?: PrecompileOptions): TemplateSpecification;
export function template(precompilation: TemplateSpecification): HandlebarsTemplateDelegate;
export function template<T = any>(precompilation: TemplateSpecification): HandlebarsTemplateDelegate<T>;
export function create(): typeof Handlebars;
@@ -96,8 +96,8 @@ interface HandlebarsTemplatable {
template: HandlebarsTemplateDelegate;
}
interface HandlebarsTemplateDelegate {
(context: any, options?: any): string;
interface HandlebarsTemplateDelegate<T = any> {
(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;
+2 -1
View File
@@ -2,6 +2,7 @@
// Project: https://github.com/gilt/koa-hbs
// Definitions by: Jacob Malone <https://github.com/jcbmln/>, Mudkip <https://github.com/mudkipme>
// 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<void>;
}
}
}
+1
View File
@@ -2,6 +2,7 @@
// Project: https://github.com/elving/swag
// Definitions by: Shogo Iwano <https://github.com/shiwano>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
/// <reference types="handlebars" />