From d165f434922d65d077c5790b2f580279a204b4d0 Mon Sep 17 00:00:00 2001 From: Yang Jun Date: Sun, 11 Jan 2026 18:37:32 +0800 Subject: [PATCH] feat: improve return types for render methods - Change render methods from returning any to string - Change internal generator methods from any to unknown - Change evalValue methods from any to unknown - Add suppressImplicitAnyIndexErrors to rollup config This improves TypeScript support for users who depend on render method return types. Fixes #832 --- rollup.config.mjs | 3 ++- src/liquid.ts | 36 ++++++++++++++++++------------------ src/render/render.ts | 2 +- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/rollup.config.mjs b/rollup.config.mjs index 3f0c95a83..9787dde73 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -23,7 +23,8 @@ const tsconfig = (target) => ({ compilerOptions: { target, module: 'ES2015', - rootDir: 'src' + rootDir: 'src', + suppressImplicitAnyIndexErrors: true } } }) diff --git a/src/liquid.ts b/src/liquid.ts index 54714bb8c..5000a1165 100644 --- a/src/liquid.ts +++ b/src/liquid.ts @@ -30,30 +30,30 @@ export class Liquid { return parser.parse(html, filepath) } - public _render (tpl: Template[], scope: Context | object | undefined, renderOptions: RenderOptions): IterableIterator { + public _render (tpl: Template[], scope: Context | object | undefined, renderOptions: RenderOptions): IterableIterator { const ctx = scope instanceof Context ? scope : new Context(scope, this.options, renderOptions) return this.renderer.renderTemplates(tpl, ctx) } - public async render (tpl: Template[], scope?: object, renderOptions?: RenderOptions): Promise { - return toPromise(this._render(tpl, scope, { ...renderOptions, sync: false })) + public async render (tpl: Template[], scope?: object, renderOptions?: RenderOptions): Promise { + return toPromise(this._render(tpl, scope, { ...renderOptions, sync: false })) as Promise } - public renderSync (tpl: Template[], scope?: object, renderOptions?: RenderOptions): any { - return toValueSync(this._render(tpl, scope, { ...renderOptions, sync: true })) + public renderSync (tpl: Template[], scope?: object, renderOptions?: RenderOptions): string { + return toValueSync(this._render(tpl, scope, { ...renderOptions, sync: true })) as string } public renderToNodeStream (tpl: Template[], scope?: object, renderOptions: RenderOptions = {}): NodeJS.ReadableStream { const ctx = new Context(scope, this.options, renderOptions) return this.renderer.renderTemplatesToNodeStream(tpl, ctx) } - public _parseAndRender (html: string, scope: Context | object | undefined, renderOptions: RenderOptions): IterableIterator { + public _parseAndRender (html: string, scope: Context | object | undefined, renderOptions: RenderOptions): IterableIterator { const tpl = this.parse(html) return this._render(tpl, scope, renderOptions) } - public async parseAndRender (html: string, scope?: Context | object, renderOptions?: RenderOptions): Promise { - return toPromise(this._parseAndRender(html, scope, { ...renderOptions, sync: false })) + public async parseAndRender (html: string, scope?: Context | object, renderOptions?: RenderOptions): Promise { + return toPromise(this._parseAndRender(html, scope, { ...renderOptions, sync: false })) as Promise } - public parseAndRenderSync (html: string, scope?: Context | object, renderOptions?: RenderOptions): any { - return toValueSync(this._parseAndRender(html, scope, { ...renderOptions, sync: true })) + public parseAndRenderSync (html: string, scope?: Context | object, renderOptions?: RenderOptions): string { + return toValueSync(this._parseAndRender(html, scope, { ...renderOptions, sync: true })) as string } public _parsePartialFile (file: string, sync?: boolean, currentFile?: string) { @@ -71,30 +71,30 @@ export class Liquid { public parseFileSync (file: string, lookupType?: LookupType): Template[] { return toValueSync(new Parser(this).parseFile(file, true, lookupType)) } - public * _renderFile (file: string, ctx: Context | object | undefined, renderFileOptions: RenderFileOptions): Generator { + public * _renderFile (file: string, ctx: Context | object | undefined, renderFileOptions: RenderFileOptions): Generator { const templates = (yield this._parseFile(file, renderFileOptions.sync, renderFileOptions.lookupType)) as Template[] return yield this._render(templates, ctx, renderFileOptions) } - public async renderFile (file: string, ctx?: Context | object, renderFileOptions?: RenderFileOptions) { - return toPromise(this._renderFile(file, ctx, { ...renderFileOptions, sync: false })) + public async renderFile (file: string, ctx?: Context | object, renderFileOptions?: RenderFileOptions): Promise { + return toPromise(this._renderFile(file, ctx, { ...renderFileOptions, sync: false })) as Promise } - public renderFileSync (file: string, ctx?: Context | object, renderFileOptions?: RenderFileOptions) { - return toValueSync(this._renderFile(file, ctx, { ...renderFileOptions, sync: true })) + public renderFileSync (file: string, ctx?: Context | object, renderFileOptions?: RenderFileOptions): string { + return toValueSync(this._renderFile(file, ctx, { ...renderFileOptions, sync: true })) as string } public async renderFileToNodeStream (file: string, scope?: object, renderOptions?: RenderOptions) { const templates = await this.parseFile(file) return this.renderToNodeStream(templates, scope, renderOptions) } - public _evalValue (str: string, scope?: object | Context): IterableIterator { + public _evalValue (str: string, scope?: object | Context): IterableIterator { const value = new Value(str, this) const ctx = scope instanceof Context ? scope : new Context(scope, this.options) return value.value(ctx) } - public async evalValue (str: string, scope?: object | Context): Promise { + public async evalValue (str: string, scope?: object | Context): Promise { return toPromise(this._evalValue(str, scope)) } - public evalValueSync (str: string, scope?: object | Context): any { + public evalValueSync (str: string, scope?: object | Context): unknown { return toValueSync(this._evalValue(str, scope)) } diff --git a/src/render/render.ts b/src/render/render.ts index 6cfc9a99a..4f7264232 100644 --- a/src/render/render.ts +++ b/src/render/render.ts @@ -11,7 +11,7 @@ export class Render { .then(() => emitter.end(), err => emitter.error(err)) return emitter.stream } - public * renderTemplates (templates: Template[], ctx: Context, emitter?: Emitter): IterableIterator { + public * renderTemplates (templates: Template[], ctx: Context, emitter?: Emitter): IterableIterator { if (!emitter) { emitter = ctx.opts.keepOutputType ? new KeepingTypeEmitter() : new SimpleEmitter() }