Skip to content

Commit 15c6fb9

Browse files
committed
Do not use declare module in first-party type declarations
First-party type declarations should simply be provided at top level in .d.ts files with the right filenames. The `declare module` was breaking usage like `import "handlebars/runtime.js"` (which happens to be the only way to import the runtime from a project with native ES modules, "type": "module"). Signed-off-by: Anders Kaseorg <[email protected]>
1 parent e914d60 commit 15c6fb9

File tree

2 files changed

+148
-152
lines changed

2 files changed

+148
-152
lines changed

runtime.d.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import Handlebars = require('handlebars')
1+
import Handlebars = require('handlebars');
22

3-
declare module "handlebars/runtime" {
4-
5-
}
3+
export = Handlebars;

types/index.d.ts

Lines changed: 146 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414
*/
1515
// TypeScript Version: 2.3
1616

17-
declare namespace Handlebars {
18-
export interface TemplateDelegate<T = any> {
17+
declare global { // global for backwards compatibility
18+
19+
namespace Handlebars {
20+
export interface TemplateDelegate<T = any> {
1921
(context: T, options?: RuntimeOptions): string;
20-
}
22+
}
2123

22-
export type Template<T = any> = TemplateDelegate<T>|string;
24+
export type Template<T = any> = TemplateDelegate<T>|string;
2325

24-
export interface RuntimeOptions {
26+
export interface RuntimeOptions {
2527
partial?: boolean;
2628
depths?: any[];
2729
helpers?: { [name: string]: Function };
@@ -34,65 +36,65 @@ declare namespace Handlebars {
3436
allowedProtoMethods?: { [name: string]: boolean };
3537
allowProtoPropertiesByDefault?: boolean;
3638
allowProtoMethodsByDefault?: boolean;
37-
}
39+
}
3840

39-
export interface HelperOptions {
41+
export interface HelperOptions {
4042
fn: TemplateDelegate;
4143
inverse: TemplateDelegate;
4244
hash: Record<string, any>;
4345
data?: any;
44-
}
46+
}
4547

46-
export interface HelperDelegate {
48+
export interface HelperDelegate {
4749
(context?: any, arg1?: any, arg2?: any, arg3?: any, arg4?: any, arg5?: any, options?: HelperOptions): any;
48-
}
49-
export interface HelperDeclareSpec {
50+
}
51+
export interface HelperDeclareSpec {
5052
[key: string]: HelperDelegate;
51-
}
53+
}
5254

53-
export interface ParseOptions {
55+
export interface ParseOptions {
5456
srcName?: string;
5557
ignoreStandalone?: boolean;
56-
}
57-
58-
export function registerHelper(name: string, fn: HelperDelegate): void;
59-
export function registerHelper(name: HelperDeclareSpec): void;
60-
export function unregisterHelper(name: string): void;
61-
62-
export function registerPartial(name: string, fn: Template): void;
63-
export function registerPartial(spec: { [name: string]: Template }): void;
64-
export function unregisterPartial(name: string): void;
65-
66-
// TODO: replace Function with actual signature
67-
export function registerDecorator(name: string, fn: Function): void;
68-
export function unregisterDecorator(name: string): void;
69-
70-
export function K(): void;
71-
export function createFrame(object: any): any;
72-
export function blockParams(obj: any[], ids: any[]): any[];
73-
export function log(level: number, obj: any): void;
74-
export function parse(input: string, options?: ParseOptions): hbs.AST.Program;
75-
export function parseWithoutProcessing(input: string, options?: ParseOptions): hbs.AST.Program;
76-
export function compile<T = any>(input: any, options?: CompileOptions): HandlebarsTemplateDelegate<T>;
77-
export function precompile(input: any, options?: PrecompileOptions): TemplateSpecification;
78-
export function template<T = any>(precompilation: TemplateSpecification): HandlebarsTemplateDelegate<T>;
79-
80-
export function create(): typeof Handlebars;
81-
82-
export const escapeExpression: typeof Utils.escapeExpression;
83-
//export const Utils: typeof hbs.Utils;
84-
export const logger: Logger;
85-
export const templates: HandlebarsTemplates;
86-
export const helpers: { [name: string]: HelperDelegate };
87-
export const partials: { [name: string]: any };
88-
// TODO: replace Function with actual signature
89-
export const decorators: { [name: string]: Function };
90-
91-
export const VERSION: string;
92-
93-
export function noConflict(): typeof Handlebars;
94-
95-
export class Exception {
58+
}
59+
60+
export function registerHelper(name: string, fn: HelperDelegate): void;
61+
export function registerHelper(name: HelperDeclareSpec): void;
62+
export function unregisterHelper(name: string): void;
63+
64+
export function registerPartial(name: string, fn: Template): void;
65+
export function registerPartial(spec: { [name: string]: Template }): void;
66+
export function unregisterPartial(name: string): void;
67+
68+
// TODO: replace Function with actual signature
69+
export function registerDecorator(name: string, fn: Function): void;
70+
export function unregisterDecorator(name: string): void;
71+
72+
export function K(): void;
73+
export function createFrame(object: any): any;
74+
export function blockParams(obj: any[], ids: any[]): any[];
75+
export function log(level: number, obj: any): void;
76+
export function parse(input: string, options?: ParseOptions): hbs.AST.Program;
77+
export function parseWithoutProcessing(input: string, options?: ParseOptions): hbs.AST.Program;
78+
export function compile<T = any>(input: any, options?: CompileOptions): HandlebarsTemplateDelegate<T>;
79+
export function precompile(input: any, options?: PrecompileOptions): TemplateSpecification;
80+
export function template<T = any>(precompilation: TemplateSpecification): HandlebarsTemplateDelegate<T>;
81+
82+
export function create(): typeof Handlebars;
83+
84+
export const escapeExpression: typeof Utils.escapeExpression;
85+
//export const Utils: typeof hbs.Utils;
86+
export const logger: Logger;
87+
export const templates: HandlebarsTemplates;
88+
export const helpers: { [name: string]: HelperDelegate };
89+
export const partials: { [name: string]: any };
90+
// TODO: replace Function with actual signature
91+
export const decorators: { [name: string]: Function };
92+
93+
export const VERSION: string;
94+
95+
export function noConflict(): typeof Handlebars;
96+
97+
export class Exception {
9698
constructor(message: string, node?: hbs.AST.Node);
9799
description: string;
98100
fileName: string;
@@ -104,15 +106,15 @@ declare namespace Handlebars {
104106
stack?: string;
105107
column?: any;
106108
endColumn?: any;
107-
}
109+
}
108110

109-
export class SafeString {
111+
export class SafeString {
110112
constructor(str: string);
111113
toString(): string;
112114
toHTML(): string;
113-
}
115+
}
114116

115-
export namespace Utils {
117+
export namespace Utils {
116118
export function escapeExpression(str: string): string;
117119
export function createFrame(object: any): any;
118120
export function blockParams(obj: any[], ids: any[]): any[];
@@ -121,13 +123,13 @@ declare namespace Handlebars {
121123
export function toString(obj: any): string;
122124
export function isArray(obj: any): boolean;
123125
export function isFunction(obj: any): boolean;
124-
}
126+
}
125127

126-
export namespace AST {
128+
export namespace AST {
127129
export const helpers: hbs.AST.helpers;
128-
}
130+
}
129131

130-
interface ICompiler {
132+
interface ICompiler {
131133
accept(node: hbs.AST.Node): void;
132134
Program(program: hbs.AST.Program): void;
133135
BlockStatement(block: hbs.AST.BlockStatement): void;
@@ -146,9 +148,9 @@ declare namespace Handlebars {
146148
UndefinedLiteral(): void;
147149
NullLiteral(): void;
148150
Hash(hash: hbs.AST.Hash): void;
149-
}
151+
}
150152

151-
export class Visitor implements ICompiler {
153+
export class Visitor implements ICompiler {
152154
accept(node: hbs.AST.Node): void;
153155
acceptKey(node: hbs.AST.Node, name: string): void;
154156
acceptArray(arr: hbs.AST.Expression[]): void;
@@ -169,103 +171,103 @@ declare namespace Handlebars {
169171
UndefinedLiteral(): void;
170172
NullLiteral(): void;
171173
Hash(hash: hbs.AST.Hash): void;
172-
}
174+
}
173175

174176

175-
export interface ResolvePartialOptions {
176-
name: string;
177-
helpers?: { [name: string]: Function };
178-
partials?: { [name: string]: HandlebarsTemplateDelegate };
179-
decorators?: { [name: string]: Function };
180-
data?: any;
177+
export interface ResolvePartialOptions {
178+
name: string;
179+
helpers?: { [name: string]: Function };
180+
partials?: { [name: string]: HandlebarsTemplateDelegate };
181+
decorators?: { [name: string]: Function };
182+
data?: any;
183+
}
184+
185+
export namespace VM {
186+
/**
187+
* @deprecated
188+
*/
189+
export function resolvePartial<T = any>(partial: HandlebarsTemplateDelegate<T> | undefined, context: any, options: ResolvePartialOptions): HandlebarsTemplateDelegate<T>;
190+
}
181191
}
182192

183-
export namespace VM {
184-
/**
185-
* @deprecated
186-
*/
187-
export function resolvePartial<T = any>(partial: HandlebarsTemplateDelegate<T> | undefined, context: any, options: ResolvePartialOptions): HandlebarsTemplateDelegate<T>;
193+
/**
194+
* Implement this interface on your MVW/MVVM/MVC views such as Backbone.View
195+
**/
196+
interface HandlebarsTemplatable {
197+
template: HandlebarsTemplateDelegate;
188198
}
189-
}
190-
191-
/**
192-
* Implement this interface on your MVW/MVVM/MVC views such as Backbone.View
193-
**/
194-
interface HandlebarsTemplatable {
195-
template: HandlebarsTemplateDelegate;
196-
}
197199

198-
// NOTE: for backward compatibility of this typing
199-
type HandlebarsTemplateDelegate<T = any> = Handlebars.TemplateDelegate<T>;
200+
// NOTE: for backward compatibility of this typing
201+
type HandlebarsTemplateDelegate<T = any> = Handlebars.TemplateDelegate<T>;
200202

201-
interface HandlebarsTemplates {
202-
[index: string]: HandlebarsTemplateDelegate;
203-
}
203+
interface HandlebarsTemplates {
204+
[index: string]: HandlebarsTemplateDelegate;
205+
}
204206

205-
interface TemplateSpecification {
207+
interface TemplateSpecification {
206208

207-
}
209+
}
208210

209-
// for backward compatibility of this typing
210-
type RuntimeOptions = Handlebars.RuntimeOptions;
211-
212-
interface CompileOptions {
213-
data?: boolean;
214-
compat?: boolean;
215-
knownHelpers?: KnownHelpers;
216-
knownHelpersOnly?: boolean;
217-
noEscape?: boolean;
218-
strict?: boolean;
219-
assumeObjects?: boolean;
220-
preventIndent?: boolean;
221-
ignoreStandalone?: boolean;
222-
explicitPartialContext?: boolean;
223-
}
211+
// for backward compatibility of this typing
212+
type RuntimeOptions = Handlebars.RuntimeOptions;
213+
214+
interface CompileOptions {
215+
data?: boolean;
216+
compat?: boolean;
217+
knownHelpers?: KnownHelpers;
218+
knownHelpersOnly?: boolean;
219+
noEscape?: boolean;
220+
strict?: boolean;
221+
assumeObjects?: boolean;
222+
preventIndent?: boolean;
223+
ignoreStandalone?: boolean;
224+
explicitPartialContext?: boolean;
225+
}
224226

225-
type KnownHelpers = {
226-
[name in BuiltinHelperName | CustomHelperName]: boolean;
227-
};
228-
229-
type BuiltinHelperName =
230-
"helperMissing"|
231-
"blockHelperMissing"|
232-
"each"|
233-
"if"|
234-
"unless"|
235-
"with"|
236-
"log"|
237-
"lookup";
238-
239-
type CustomHelperName = string;
240-
241-
interface PrecompileOptions extends CompileOptions {
242-
srcName?: string;
243-
destName?: string;
244-
}
227+
type KnownHelpers = {
228+
[name in BuiltinHelperName | CustomHelperName]: boolean;
229+
};
230+
231+
type BuiltinHelperName =
232+
"helperMissing"|
233+
"blockHelperMissing"|
234+
"each"|
235+
"if"|
236+
"unless"|
237+
"with"|
238+
"log"|
239+
"lookup";
240+
241+
type CustomHelperName = string;
242+
243+
interface PrecompileOptions extends CompileOptions {
244+
srcName?: string;
245+
destName?: string;
246+
}
245247

246-
declare namespace hbs {
247-
// for backward compatibility of this typing
248-
type SafeString = Handlebars.SafeString;
248+
namespace hbs {
249+
// for backward compatibility of this typing
250+
type SafeString = Handlebars.SafeString;
249251

250-
type Utils = typeof Handlebars.Utils;
251-
}
252+
type Utils = typeof Handlebars.Utils;
253+
}
252254

253-
interface Logger {
254-
DEBUG: number;
255-
INFO: number;
256-
WARN: number;
257-
ERROR: number;
258-
level: number;
255+
interface Logger {
256+
DEBUG: number;
257+
INFO: number;
258+
WARN: number;
259+
ERROR: number;
260+
level: number;
259261

260-
methodMap: { [level: number]: string };
262+
methodMap: { [level: number]: string };
261263

262-
log(level: number, obj: string): void;
263-
}
264+
log(level: number, obj: string): void;
265+
}
264266

265-
type CompilerInfo = [number/* revision */, string /* versions */];
267+
type CompilerInfo = [number/* revision */, string /* versions */];
266268

267-
declare namespace hbs {
268-
namespace AST {
269+
namespace hbs {
270+
namespace AST {
269271
interface Node {
270272
type: string;
271273
loc: SourceLocation;
@@ -410,13 +412,9 @@ declare namespace hbs {
410412
scopeId(path: PathExpression): boolean;
411413
simpleId(path: PathExpression): boolean;
412414
}
415+
}
413416
}
414-
}
415417

416-
declare module "handlebars" {
417-
export = Handlebars;
418418
}
419419

420-
declare module "handlebars/runtime" {
421-
export = Handlebars;
422-
}
420+
export = Handlebars;

0 commit comments

Comments
 (0)