refactor: Centralize template render type assertion (#2538)

This commit is contained in:
Dermot Duffy
2026-06-30 17:45:13 -07:00
committed by dermotduffy
parent a31816c168
commit 5572ec728e
5 changed files with 44 additions and 16 deletions
+11
View File
@@ -31,6 +31,17 @@ export class TemplateRenderer {
);
};
// Structure-preserving variant of `renderRecursively`: arrays, records, and
// primitives keep their shape (only string leaves are rendered), so the
// caller's type is asserted back unchanged. Callers whose template renders to
// a *different* type than its input (e.g. a string that yields a boolean)
// must use `renderRecursively` and narrow the `unknown` result at runtime.
public renderRecursivelyAsType = <T>(
hass: HomeAssistant,
data: T,
options?: TemplateRenderOptions,
): T => this.renderRecursively(hass, data, options) as T;
private _generateTemplateContext(
options?: TemplateRenderOptions,
): TemplateContext | undefined {