Skip to content

Commit 3587892

Browse files
committed
fix(core): make Template a string primitive
Signed-off-by: 90dy <90dy@proton.me>
1 parent 85a6823 commit 3587892

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

src/core/mod.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,19 @@ export const LANGUAGES: Record<string, LanguageDefinition> = {
300300
},
301301
};
302302

303-
class TemplateDocument<T> extends String {
303+
type Template<T> = string & {
304+
readonly type: string;
305+
readonly raw: string;
306+
readonly data?: unknown;
307+
indent: (value: false | number) => Template<T>;
308+
noindent: () => Template<T>;
309+
throw: () => never;
310+
parse: <TParsed = T>(
311+
parser?: (text: string) => TParsed,
312+
) => Template<TParsed>;
313+
}
314+
315+
class TemplateClass<T = unknown> extends String {
304316
public readonly type: string;
305317
public readonly raw: string;
306318
public data?: T;
@@ -350,36 +362,36 @@ class TemplateDocument<T> extends String {
350362
return this.valueOf();
351363
}
352364

353-
indent(value: false | number): TemplateDocument<T> {
354-
return new TemplateDocument<T>(
365+
indent(value: false | number): TemplateClass<T> {
366+
return new TemplateClass<T>(
355367
this.type,
356368
{ raw: [this.raw] },
357369
[],
358370
undefined,
359371
{
360372
indent: value,
361373
},
362-
);
374+
)
363375
}
364-
noindent(): TemplateDocument<T> {
376+
noindent(): TemplateClass<T> {
365377
return this.indent(false);
366378
}
367-
throw(): TemplateDocument<T> {
379+
throw(): TemplateClass<T> | never {
368380
if (this.error) {
369381
throw this.error;
370382
}
371-
return this;
383+
return this
372384
}
373385
parse<TParsed = T>(
374386
parser?: (text: string) => TParsed,
375-
): TemplateDocument<TParsed> {
387+
): TemplateClass<TParsed> {
376388
this.parser = parser ?? this.parser;
377389
try {
378390
this.data = this.parser?.(this.raw) as T;
379391
} catch (error) {
380392
this.error = error as Error;
381393
}
382-
return this as unknown as TemplateDocument<TParsed>;
394+
return this as unknown as TemplateClass<TParsed>;
383395
}
384396
}
385397

@@ -399,13 +411,13 @@ export function ext<Type extends string>(
399411
return (
400412
template: { raw: readonly string[] | ArrayLike<string> },
401413
...substitutions: any[]
402-
) => new TemplateDocument(type, template, substitutions, parser, options);
414+
) => new TemplateClass(type, template, substitutions, parser, options) as Template<Type>;
403415
}
404416

405417
type Ext<Type extends string> = <T>(
406418
template: { raw: readonly string[] | ArrayLike<string> },
407419
...substitutions: any[]
408-
) => TemplateDocument<T>;
420+
) => Template<T>;
409421

410422
// Export all template functions individually
411423
// Web languages

0 commit comments

Comments
 (0)