Repository navigation
Conversation
precompile returns a string, or { code, map } when srcName is set, and never a TemplateSpecification.
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
The precompile overloads incorrectly guarantee a string map and an object result for empty srcName values, while rejecting valid calls that forward optional options.
No open issues 🎉
What changed in this PR
This PR updates Handlebars’ TypeScript declarations to better reflect precompile’s runtime return values, addressing #2198 without changing runtime code.
Changes:
- Replaces the
TemplateSpecificationreturn type with overloads based onsrcName, including a union for broadly typed options. - Updates type tests to cover plain source, source-map output, and uncertain options.
| File | Description |
|---|---|
types/index.d.ts |
Adds srcName-dependent precompile return-type overloads. |
types/__typetests__/handlebars.tst.ts |
Replaces the old return-type assertion with tests for the three overload forms. |
Review effort: Balanced
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
types/index.d.tsdeclares thatprecompilereturns aTemplateSpecification, but at runtime it returns one of two other values:srcName, it returns a string of JavaScript source.srcName, it returns{ code, map }, wheremapis a serialized source map.This pull request replaces the declaration with overloads that follow
srcName:optionssrcNamestringsrcName: string{ code: string; map: string }PrecompileOptionsvalue, wheresrcNamemay or may not be setstring | { code: string; map: string }The third overload goes slightly beyond the suggestion in the issue. Without it, a value typed as
PrecompileOptionswould either fail to compile or be reported as astringeven when it holds asrcName.Fixes #2198
Compatibility
This changes only the type declarations. Code that relies on the previous return type, for example by assigning the result to a
TemplateSpecificationvariable, continues to compile becauseTemplateSpecificationis an empty interface. For the same reason, passing the result ofprecompiledirectly toHandlebars.template()still compiles. Addressing that would mean givingTemplateSpecificationa real shape, which I have left out of this change.Tests
In
types/__typetests__/handlebars.tst.ts, I replaced the test that recorded the old return type with tests for each of the three forms above.Verification
Results on Node.js 22:
pnpm run lint:typespasses on this branch, with 67 tests passing. The three new tests fail against the previous declaration.oxfmt --checkpasses on the two changed files.pnpm run buildsucceeds, and the built package returns the runtime values described above:stringwithoutsrcName, and an object with the keyscodeandmapwithsrcName.I did not run the runtime test suites, as no runtime code changes. I am relying on CI for those.