Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript typings #5

Open
MickL opened this issue Apr 5, 2022 · 2 comments
Open

TypeScript typings #5

MickL opened this issue Apr 5, 2022 · 2 comments

Comments

@MickL
Copy link

MickL commented Apr 5, 2022

Handlebars contains TypeScript typings which totally get lost by using this module. Could you add typings that extend handlebars?

@gastonrobledo
Copy link
Owner

You can do it if you want, and submit a PR ;)

@toverux
Copy link

toverux commented Oct 19, 2022

Hi, for people like me who can't bear using an any-typed library :

declare module 'handlebars-async-helpers' {
    import * as hbs from 'handlebars';

    type Handlebars = typeof hbs;

    function asyncHelpers(handlebars: Handlebars): asyncHelpers.AsyncHandlebars;

    namespace asyncHelpers {
        // At least an empty namespace is necessary for allowing "import * as" when allowSyntheticDefaultImports is
        // false. But we'll export our custom types too, so they can be imported too, they can be useful.

        type AsyncTemplateDelegate<Context> = (context: Context, options?: hbs.RuntimeOptions) => Promise<string>;

        type AsyncHandlebars = Omit<Handlebars, 'compile'> & {
            // CompileOptions is not explicitly imported because it's transient in the scope due to how it's declared in
            // Handlebars.
            compile<Context = unknown>(input: any, options?: CompileOptions): AsyncTemplateDelegate<Context>;
        }
    }

    export = asyncHelpers;
}

Put this in a .d.ts file that your TypeScript tsconfig.json is configured to read, ex:

{
  "include": ["./stubs/**.d.ts"] // That's where I place my .d.ts for untyped libraries
}

If you have allowSyntheticDefaultImports enabled: import asyncHelpers from 'handlebars-async-helpers';
If not: import * as asyncHelpers from 'handlebars-async-helpers';

Note to the author: I don't have time to make a PR but feel free to include this in your project if you want. Basically you'll just have to add a types entry pointing to the .d.ts in your published package.json.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants