- Courses
- đź’»Programming Languages
- 🛡️TypeScript
- Interfaces, Classes & OOP in TypeScript
- Hands‑On: Build a Typed Plugin System Using Interfaces
On this page
Guided Exercise: Build a Typed Plugin System Using TypeScript Interfaces
Published on August 3, 2026
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
This guided exercise walks you through building a complete, type-safe plugin system in TypeScript. A plugin system is a common architectural pattern where a core application loads and runs extensions (plugins) that all follow a shared contract. In this exercise, you will define an interface that every plugin must implement, create a manager that registers and runs plugins, implement concrete plugins as classes, and discover how TypeScript's structural typing allows even plain objects to act as plugins. Every step is fully explained, and the code is provided so you can follow along without guessing.
Step 1: Define the Plugin Interface​
The first step in building any plugin system is to define the contract that all plugins must follow. This contract is an interface that specifies what properties and methods a plugin must have.
Create a file named plugin.ts:
export interface Plugin {
name: string;
initialize(): void;
execute(data: string): string;
}
This interface defines the contract every plugin must follow:
name— a string that identifies the plugininitialize()— a method that performs any setup logic the plugin needsexecute(data: string)— a method that processes input data and returns a transformed string
The Plugin interface is the foundation of the entire system. Any plugin that does not implement these correctly will fail at compile time.
Step 2: Create the Plugin Manager​
The plugin manager is responsible for storing registered plugins, initializing them, and running them. It interacts with plugins only through the Plugin interface, which means it doesn't care about the internal details of each plugin — only that they satisfy the contract.
Create a file named pluginManager.ts:
import { type Plugin } from "./plugin";
export class PluginManager {
private plugins: Plugin[] = [];
register(plugin: Plugin) {
this.plugins.push(plugin);
}
initializeAll() {
for (const p of this.plugins) {
p.initialize();
}
}
runAll(data: string): string[] {
return this.plugins.map((p) => p.execute(data));
}
}
The PluginManager class has three main methods:
register(plugin: Plugin)— adds a plugin to the internal list. The parameter type isPlugin, so only objects that satisfy the interface can be registered.initializeAll()— loops through all registered plugins and calls theirinitializemethod. This ensures every plugin has a chance to set up before execution.runAll(data: string)— loops through all plugins, callsexecuteon each, and returns an array of the results. Thedatastring is passed unchanged to every plugin.
Because the manager uses the Plugin interface, it doesn't care which plugin it receives — only that it satisfies the contract. This is the essence of polymorphism.
Step 3: Implement Concrete Plugins​
Now you can create plugins that implement the Plugin interface. Each plugin provides its own implementation of initialize and execute.
Create a file named uppercasePlugin.ts:
import { type Plugin } from "./plugin";
export class UppercasePlugin implements Plugin {
name = "uppercase";
initialize() {
console.log("Uppercase plugin initialized");
}
execute(data: string): string {
return data.toUpperCase();
}
}
The UppercasePlugin class implements the Plugin interface. Its initialize method logs a message, and its execute method converts the input string to uppercase.
Create another plugin named reversePlugin.ts:
import { type Plugin } from "./plugin";
export class ReversePlugin implements Plugin {
name = "reverse";
initialize() {
console.log("Reverse plugin initialized");
}
execute(data: string): string {
return data.split("").reverse().join("");
}
}
The ReversePlugin class also implements the Plugin interface. Its execute method reverses the input string.
Both plugins implement the same interface but behave differently. This is polymorphism — different objects that share a common interface can be treated uniformly by the manager.
Step 4: Use the Plugin System​
Now you can wire everything together in the main application file.
Create a file named index.ts:
import { PluginManager } from "./pluginManager";
import { UppercasePlugin } from "./uppercasePlugin";
import { ReversePlugin } from "./reversePlugin";
const manager = new PluginManager();
manager.register(new UppercasePlugin());
manager.register(new ReversePlugin());
manager.initializeAll();
const results = manager.runAll("hello world");
console.log(results);
Run the code with ts-node index.ts. The output will be:
Uppercase plugin initialized
Reverse plugin initialized
[ 'HELLO WORLD', 'dlrow olleh' ]
The manager registered both plugins, initialized them (which logged the messages), and then ran them on the input "hello world". The results array contains the transformed strings from each plugin.
Step 5: Add a Third-Party Plugin Using Structural Typing​
Here is where TypeScript's structural typing shines. A plugin does not need to explicitly implement the interface with the implements keyword. If an object has the right shape — the same properties and methods — TypeScript will accept it as a plugin.
Create a file named customPlugin.ts:
export const CustomPlugin = {
name: "custom",
initialize() {
console.log("Custom plugin ready");
},
execute(data: string): string {
return `*** ${data} ***`;
},
};
This is a plain object, not a class. It does not use implements. But because it has the same shape as the Plugin interface — a name string, an initialize method, and an execute method — TypeScript considers it compatible.
Now import and register it in index.ts:
import { CustomPlugin } from "./customPlugin";
// ... after registering the other plugins
manager.register(CustomPlugin);
The CustomPlugin works exactly like the class-based plugins. This is incredibly powerful for plugin ecosystems, because third-party developers can provide plugins as simple objects without needing to import or extend any base classes.
Step 6: Add Optional Capabilities​
In real-world plugin systems, some capabilities are optional. You can extend the interface with optional properties and methods.
Update plugin.ts to include an optional cleanup method:
export interface Plugin {
name: string;
initialize(): void;
execute(data: string): string;
cleanup?(): void;
}
Plugins may implement cleanup, but they don't have to. This is marked with a ? to indicate it is optional.
Update the PluginManager to call cleanup on plugins that have it:
shutdown() {
for (const p of this.plugins) {
p.cleanup?.();
}
}
The optional chaining operator ?. calls cleanup only if it exists on the plugin. This pattern allows the system to evolve with new capabilities without breaking existing plugins.
Step 7: Add a Plugin That Uses cleanup​
Create a plugin that implements the optional cleanup method:
import { type Plugin } from "./plugin";
export class LoggingPlugin implements Plugin {
name = "logger";
initialize() {
console.log("Logger started");
}
execute(data: string): string {
console.log("Processing:", data);
return data;
}
cleanup() {
console.log("Logger shutting down");
}
}
When the plugin system shuts down, this plugin will log a message, allowing it to release resources or save state.
This demonstrates how optional members make the interface extensible — new capabilities can be added over time without breaking existing plugins.
What You Learned​
This guided exercise demonstrates how TypeScript makes plugin architectures safe and expressive.
1. Interfaces enforce contracts
- Plugins must implement required methods
- Compile-time errors catch missing or incorrect implementations
2. Classes provide structure
- Encapsulation
- Reusable logic
- Clear initialization and execution flows
3. Structural typing enables flexible polymorphism
- Plugins don't need to explicitly implement the interface
- Any object with the right shape works
- Perfect for third-party extensions
4. Optional members allow extensibility
- Add new capabilities without breaking existing plugins
- Real-world pattern used in frameworks and libraries
This is exactly how real plugin systems are built in TypeScript ecosystems. The type system ensures that plugins are safe and predictable, while structural typing makes the system flexible and easy to extend.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Implementing Interfaces
Next
Generics
- Step 1: Define the Plugin Interface
- Step 2: Create the Plugin Manager
- Step 3: Implement Concrete Plugins
- Step 4: Use the Plugin System
- Step 5: Add a Third-Party Plugin Using Structural Typing
- Step 6: Add Optional Capabilities
- Step 7: Add a Plugin That Uses
cleanup - What You Learned
