-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Closed
Description
Before filing issues, please check the following points first:
- Please don't open issues for security issues. Instead, file a report at https://www.npmjs.com/advisories/report?package=handlebars
- Have a look at https://github.com/wycats/handlebars.js/blob/master/CONTRIBUTING.md
- Read the FAQ at https://github.com/wycats/handlebars.js/blob/master/FAQ.md
- Use the jsfiddle-template at https://jsfiddle.net/9D88g/47/ to reproduce problems or bugs
This will probably help you to get a solution faster.
For bugs, it would be great to have a PR with a failing test-case.
Currently Handlebar's typescript definition don't allow to specify custom helpers on the knownHelper
option.
So, to use the knownHelpersOnly
option with custom helpers, you have to cast the knownHelpers
in order to by pass typescript checks;
function given(...args: any[]): string {
const options = args.pop();
if (options.fn) {
let complete = true;
const proxy = new Proxy(this, {
get(context, prop: string): object {
if (!(prop in context)) {
complete = false;
}
return context[prop];
},
});
const text = options.fn(proxy);
return complete ? text : '';
}
return args.some((a: string): boolean => !a) ? '' : args.join(' ');
}
const templateOptions = {
helpers: {
first,
},
};
const compilerOptions = {
knownHelpers: { first: true } as unknown, // Without this `unknown` compilation fails
knownHelpersOnly: true,
};
Hanblebars.compile('My nice template {{first "text"}}', compilerOptions)({}, templateOptions);
The produced error (without using the unknown
cast) is the following:
TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
src/index.test.ts:93:41 - error TS2345: Argument of type '{ knownHelpers: { first: boolean; }; knownHelpersOnly: boolean; }' is not assignable to parameter of type 'CompileOptions'.
Types of property 'knownHelpers' are incompatible.
Type '{ first: boolean; }' has no properties in common with type '{ helperMissing?: boolean; blockHelperMissing?: boolean; each?: boolean; if?: boolean; unless?: boolean; with?: boolean; log?: boolean; lookup?: boolean; }'.
Shouldn't the CompilerOptions
have the follow definition?:
interface CompileOptions {
data?: boolean;
compat?: boolean;
knownHelpers?: { [name: string]: boolean };
knownHelpersOnly?: boolean;
noEscape?: boolean;
strict?: boolean;
assumeObjects?: boolean;
preventIndent?: boolean;
ignoreStandalone?: boolean;
explicitPartialContext?: boolean;
}
(Am I not getting something?. I'm willing to send the PR if this is the right fix)
Metadata
Metadata
Assignees
Labels
No labels