Template Method Pattern Implementation in TypeScript
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Template Method Pattern runs in O(1) time and O(1) space, in TypeScript as in every other language.
- ✓The abstract keyword on a class and on its members gives compile-time enforcement that JavaScript lacks: an abstract class cannot be instantiated and a concrete subclass that omits an abstract member fails to compile, at the class declaration rather than at construction.
- ✓Declare the template method neither abstract nor overridable and mark hooks protected abstract, so the shape of the pattern is expressed in the type system rather than in comments.
- ✓The TypeScript trap: TypeScript has no final, so nothing prevents a subclass from overriding the template method and destroying the invariant the pattern exists to protect; the usual workarounds are a private method the subclass cannot see plus a public wrapper, or a lint rule.
- ✓All erased, so the runtime behaviour is the JavaScript prototype dispatch. The useEffect of noImplicitOverride and abstract members is purely compile-time.
What Template Method Pattern actually is
Template Method Pattern in TypeScript: what you actually reach for
TypeScript implementation
abstract class Importer<TRow> {
// Not abstract, not intended for override, TypeScript has no 'final',
// so the only real protection is making the body private.
run(path: string): number {
const raw = this.read(path);
const rows = this.transform(raw);
this.after(rows);
return rows.length;
}
protected abstract read(path: string): string;
protected abstract transform(raw: string): TRow[];
protected after(rows: TRow[]): void {} // optional hook with a default
}
class CsvImporter extends Importer<string[]> {
private separator = ',';
// 'override' is REQUIRED under noImplicitOverride: a typo is a compile error.
protected override read(path: string): string { return 'a,b\nc,d' }
protected override transform(raw: string): string[][] {
return raw.split('\n').map((l) => l.split(this.separator));
}
}
// A concrete subclass that omits a step fails HERE, at the declaration:
// class Broken extends Importer<string> {}
// error: non-abstract class 'Broken' does not implement 'read', 'transform'Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe TypeScript-specific trap
Performance in TypeScript, beyond the O(1)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which TypeScript type or library should I use for Template Method Pattern?
The abstract keyword on a class and on its members gives compile-time enforcement that JavaScript lacks: an abstract class cannot be instantiated and a concrete subclass that omits an abstract member fails to compile, at the class declaration rather than at construction. protected restricts hooks to the hierarchy. Since TypeScript 4.3, override can be required with the noImplicitOverride compiler option, which makes an accidental shadow a compile error, and abstract construct signatures allow an abstract class to be passed as a factory type.
What is the most common TypeScript mistake when implementing Template Method Pattern?
TypeScript has no final, so nothing prevents a subclass from overriding the template method and destroying the invariant the pattern exists to protect; the usual workarounds are a private method the subclass cannot see plus a public wrapper, or a lint rule. The other trap is the same class-field ordering problem as JavaScript, and it is easier to hit here because the field looks initialised in the declaration: a base constructor calling a template method sees subclass fields as undefined despite their types claiming otherwise, which is a genuine hole in the type system rather than a lint issue.
How does Template Method Pattern actually perform in TypeScript?
All erased, so the runtime behaviour is the JavaScript prototype dispatch. The useEffect of noImplicitOverride and abstract members is purely compile-time. One emit detail matters: with useDefineForClassFields set to true, which is the default for modern targets, class fields are emitted as defineProperty rather than assignment, which changes shadowing behaviour with base-class accessors and can break a template method that relies on a getter being overridden by a field. Time complexity is O(1) and space is O(1).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialIf you are a student, see what Replit offers students for free on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.