Skip to content

Commit

Permalink
Improve promised-handlebars types
Browse files Browse the repository at this point in the history
Change-type: patch
  • Loading branch information
pipex committed Jan 10, 2025
1 parent 9de89bc commit 04d6828
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
8 changes: 3 additions & 5 deletions lib/partials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ hb.registerHelper('import', async (options) => {
const safeContent = new hb.SafeString(
partialContent.slice(0, partialContent.length - 1),
);
const builtContent = await Promise.resolve(
hb.compile(safeContent.toString())(options.data.root),
const builtContent = await hb.compile(safeContent.toString())(
options.data.root,
);
return new hb.SafeString(builtContent);
} catch (err) {
Expand Down Expand Up @@ -248,7 +248,5 @@ export const buildTemplate = async (
context.toJSON().children,
);

return stripExtraBlankLines(
await Promise.resolve(hb.compile(template)(data)),
);
return stripExtraBlankLines(await hb.compile(template)(data));
};
27 changes: 25 additions & 2 deletions typings/promised-handlebars.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
declare module 'promised-handlebars' {
import type Handlebars from 'handlebars';
import type HandlebarsNS from 'handlebars';

const fn: (hb: typeof Handlebars) => typeof Handlebars;
// Extend the Handlebars type to include an async `compile` method
type Handlebars = typeof HandlebarsNS;

// Convert the template delegate to async
// Note: this was tested with handlebars 4.7.8
type HandlebarsTemplateDelegate<T> = Handlebars.TemplateDelegate<T>;
type HandlebarsTemplateDelegateArgs<T> = Parameters<
HandlebarsTemplateDelegate<T>
>;
type HandlebarsTemplateDelegateReturn<T> = ReturnType<
HandlebarsTemplateDelegate<T>
>;
type HandlebarsTemplateDelegateAsync<T> = (
...args: HandlebarsTemplateDelegateArgs<T>
) => Promise<HandlebarsTemplateDelegateReturn<T>>;

// Create a new Handlebars interface with an updated compile function
type CompileArgs = Parameters<Handlebars['compile']>;
interface HandlebarsAsync extends Handlebars {
compile: <T>(...args: CompileArgs) => HandlebarsTemplateDelegateAsync<T>;
}

// Define the function exported by the module
const fn: (hb: Handlebars) => HandlebarsAsync;
export = fn;
}

0 comments on commit 04d6828

Please sign in to comment.