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 types: knownHelpers doesnt allow for custom helpers #1544

Closed
4 tasks done
NickCis opened this issue Aug 28, 2019 · 4 comments
Closed
4 tasks done

Typescript types: knownHelpers doesnt allow for custom helpers #1544

NickCis opened this issue Aug 28, 2019 · 4 comments

Comments

@NickCis
Copy link

NickCis commented Aug 28, 2019

Before filing issues, please check the following points first:

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)

nknapp added a commit that referenced this issue Aug 29, 2019
- Add support for custom helpers while keeping
  the list of builtin-helpers to maintain for
  autocompletion support

closes #1544
@nknapp
Copy link
Collaborator

nknapp commented Aug 29, 2019

Thanks for the offer. I have pushed a slightly different fix. I think in order to get proper auto-completion for the builtin-helpers in IDEs, it is better to keep the builtin names, but still allow any other name.

@NickCis
Copy link
Author

NickCis commented Aug 29, 2019

Thanks for the quick fix! In which version will it be released?

@NickCis NickCis closed this as completed Aug 29, 2019
@nknapp
Copy link
Collaborator

nknapp commented Aug 31, 2019

Next patch version. I am thinking what would be a good time to do it. I'm planning Tuesday for the next release.

@nknapp
Copy link
Collaborator

nknapp commented Sep 3, 2019

Released in 4.2.0

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

2 participants