Skip to content

Commit 16572cd

Browse files
committed
feat: enhance typescript definition for knownHelpers
- Add support for custom helpers while keeping the list of builtin-helpers to maintain for autocompletion support closes #1544
1 parent f98c6a5 commit 16572cd

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

types/index.d.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,7 @@ type RuntimeOptions = Handlebars.RuntimeOptions;
173173
interface CompileOptions {
174174
data?: boolean;
175175
compat?: boolean;
176-
knownHelpers?: {
177-
helperMissing?: boolean;
178-
blockHelperMissing?: boolean;
179-
each?: boolean;
180-
if?: boolean;
181-
unless?: boolean;
182-
with?: boolean;
183-
log?: boolean;
184-
lookup?: boolean;
185-
};
176+
knownHelpers?: KnownHelpers;
186177
knownHelpersOnly?: boolean;
187178
noEscape?: boolean;
188179
strict?: boolean;
@@ -192,6 +183,22 @@ interface CompileOptions {
192183
explicitPartialContext?: boolean;
193184
}
194185

186+
type KnownHelpers = {
187+
[name in BuiltinHelperName | CustomHelperName]: boolean;
188+
};
189+
190+
type BuiltinHelperName =
191+
"helperMissing"|
192+
"blockHelperMissing"|
193+
"each"|
194+
"if"|
195+
"unless"|
196+
"with"|
197+
"log"|
198+
"lookup";
199+
200+
type CustomHelperName = string;
201+
195202
interface PrecompileOptions extends CompileOptions {
196203
srcName?: string;
197204
destName?: string;

types/test.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Handlebars.registerHelper('list', (context, options: Handlebars.HelperOptions) =
7676
}
7777
return ret + "</ul>";
7878
});
79-
template6([{url:"", title:""}])
79+
template6([{url:"", title:""}]);
8080

8181

8282
const escapedExpression = Handlebars.Utils.escapeExpression('<script>alert(\'xss\');</script>');
@@ -88,4 +88,12 @@ const parsedTmpl = Handlebars.parse('<p>Hello, my name is {{name}}.</p>', {
8888
ignoreStandalone: true
8989
});
9090

91-
const parsedTmplWithoutOptions = Handlebars.parse('<p>Hello, my name is {{name}}.</p>');
91+
const parsedTmplWithoutOptions = Handlebars.parse('<p>Hello, my name is {{name}}.</p>');
92+
93+
// #1544, allow custom helpers in knownHelpers
94+
Handlebars.compile('test', {
95+
knownHelpers: {
96+
each: true,
97+
customHelper: true
98+
}
99+
});

0 commit comments

Comments
 (0)