Strategy 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
- ✓Strategy Pattern runs in O(1) time and O(1) space, in TypeScript as in every other language.
- ✓A type alias for the function signature is the whole interface: type Pricing = (cents: number) => number.
- ✓Declare the strategy as a function type rather than an interface with a single call member unless you need to attach properties to it; reviewers treat the one-method interface as needless ceremony.
- ✓The TypeScript trap: Function parameter positions are checked BIVARIANTLY for method-shaped declarations even under strictFunctionTypes, which is a deliberate compatibility carve-out: writing the strategy as a method in an interface silently accepts a handler that takes a narrower parameter than it will actually receive, and the failure appears at runtime.
- ✓All of this erases, so the runtime cost is the JavaScript cost and the polymorphic-call-site concern applies unchanged.
What Strategy Pattern actually is
Strategy Pattern in TypeScript: what you actually reach for
TypeScript implementation
type Cents = number;
type Pricing = (cents: Cents) => Cents; // property-style: STRICT checking
type Kind = 'flat' | 'percent';
const pricing = {
flat: (c: Cents) => Math.max(0, c - 100),
percent: (c: Cents) => Math.floor(c * 0.9),
} satisfies Record<Kind, Pricing>; // a new Kind breaks the BUILD
function total(items: readonly Cents[], p: Pricing): Cents {
return items.reduce((sum, c) => sum + p(c), 0);
}
// The bivariance carve-out, side by side:
interface MethodStyle { handle(e: MouseEvent): void } // bivariant, LOOSE
interface PropStyle { handle: (e: MouseEvent) => void } // contravariant, STRICT
declare const loose: MethodStyle;
declare const strict: PropStyle;
// loose.handle may be assigned a (e: Event) => void; strict.handle may not.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 Strategy Pattern?
A type alias for the function signature is the whole interface: type Pricing = (cents: number) => number. A union of string literal keys plus Record<Key, Strategy> gives a registry whose completeness the compiler checks. Generic constraints let one strategy type serve many element types, and TypeScript 4.7 added instantiation expressions for pre-specialising a generic function into a concrete strategy without calling it.
What is the most common TypeScript mistake when implementing Strategy Pattern?
Function parameter positions are checked BIVARIANTLY for method-shaped declarations even under strictFunctionTypes, which is a deliberate compatibility carve-out: writing the strategy as a method in an interface silently accepts a handler that takes a narrower parameter than it will actually receive, and the failure appears at runtime. Declaring the same thing as a property with a function type gets the strict, contravariant check. The two spellings look interchangeable and are not.
How does Strategy Pattern actually perform in TypeScript?
All of this erases, so the runtime cost is the JavaScript cost and the polymorphic-call-site concern applies unchanged. What does cost is compile time: a strategy type built from conditional or mapped types and instantiated across a large union makes tsc quadratic in the union size in the worst case, and editors feel it first. Keep the generic surface of a widely used strategy type shallow. 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 trialWeighing more options? Compare the best software in each category 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.