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

registerHelper breaks with Typescript 4.4 #1767

Closed
4 tasks done
Fadorico opened this issue Sep 14, 2021 · 7 comments · Fixed by handlebars-lang/docs#128
Closed
4 tasks done

registerHelper breaks with Typescript 4.4 #1767

Fadorico opened this issue Sep 14, 2021 · 7 comments · Fixed by handlebars-lang/docs#128

Comments

@Fadorico
Copy link

Fadorico commented Sep 14, 2021

Typescript 4.4 as recently introduced this breaking change: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-4.html#more-compliant-indirect-calls-for-imported-functions

This change breaks the registerHelper function, and possibly other functions as well. Taking the following example code:

registerHelper('myHelper', function (value, options) {});

This gets transpiled into the following:

(0, handlebars_1.registerHelper)('myHelper', function (value, options) {});

Which causes the following error:

Cannot read property 'helpers' of undefined
at registerHelper ([...]/node_modules/handlebars/lib/handlebars/base.js:47:12)

This is using the current latest version of Handlebars (4.7.7) and Typescript 4.4.3

@nknapp
Copy link
Collaborator

nknapp commented Dec 4, 2021

Can you provide an example reproducing the bug? I mean a minimal but complete js-file, including the import?

Does it work when you do?

import Handlebars from 'handlebars'

Handlebars.registerHelper('...')

or

import * as Handlebars from 'handlebars'

Handlebars.registerHelper('...')

@Fadorico
Copy link
Author

Fadorico commented Dec 6, 2021

Both cases that you provided seem to work. The issue seems to only happen if you import it like so:
import { registerHelper } from 'handlebars';

Here's a minimal ts file that reproduces the problem:

import { registerHelper } from 'handlebars';

registerHelper('myHelper', function (value, options) {});

And here's the transpiled version of it

"use strict";

Object.defineProperty(exports, "__esModule", { value: true });

const handlebars_1 = require("handlebars");

(0, handlebars_1.registerHelper)('myHelper', function (value, options) { });

@webjoyable
Copy link

I have the same issue, with named import something breaks

handlebars@4.7.7
typescript@4.6.0

TypeError: Cannot read property 'helpers' of undefined at registerHelper ([...]\node_modules\handlebars\dist\cjs\handlebars\base.js:68:12)

@jaylinski
Copy link
Member

As far as I can tell, the Handlebars-prototype functions (like registerHelper) should have never been exposed (exported) via .d.ts.

I will create a PR to fix those types.

Workaround: always use Handlebars (import Handlebars from 'handlebars') to access functions.

jaylinski added a commit that referenced this issue Jan 4, 2022
@carlos-reta
Copy link

I have similar issue when using typescript > 4.3.X

/Users/carlosreta/git/code-generator/test/uxf-seed/node_modules/handlebars/dist/cjs/handlebars/base.js:66
_utils.extend(this.helpers, name);
^

TypeError: Cannot read properties of undefined (reading 'helpers')
at registerHelper (/Users/carlosreta/git/code-generator/test/uxf-seed/node_modules/handlebars/dist/cjs/handlebars/base.js:66:26)
at registerComparisonHelpers (/Users/carlosreta/git/code-generator/test/uxf-seed/node_modules/@clx/uxf-code-generator/src/generators/register-helpers.js:16:37)
at registerHelpers (/Users/carlosreta/git/code-generator/test/uxf-seed/node_modules/@clx/uxf-code-generator/src/generators/register-helpers.js:93:5)
at Object. (/Users/carlosreta/git/code-generator/test/uxf-seed/node_modules/@clx/uxf-code-generator/src/index.js:79:40)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47

With typescript 4.3.5 works but with typescript 4.6.2 f.ex. gives this error.

@bjornlll
Copy link

bjornlll commented Jan 16, 2023

Workaround: always use Handlebars (import Handlebars from 'handlebars') to access functions.

Fixed it for me. Thanks @jaylinski !

Is this in the docs? If it isn't it should be. Took me 10 mins of random googling to figure this one out.

@jaylinski
Copy link
Member

I opened a PR that adds an ESM example to the docs:

handlebars-lang/docs#128

Sadly I doesn't seem to be possible to write a TypeScript definition that forbids importing Handlebars like import { registerHelper } from 'handlebars'; while still being compatible with ESM and CommonJS. (At least I couldn't get it to work.)

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

Successfully merging a pull request may close this issue.

6 participants