Objects & Interfaces
Hybrid Types (Callable Interfaces)
Master hybrid types in TypeScript, callable interfaces, construct signatures, function properties, and typing complex JavaScript legacy utilities like jQuery or Express.
Because JavaScript is dynamic, an object can simultaneously act as both a callable function AND an object with attached properties. In TypeScript, an interface that describes a callable object with properties is called a Hybrid Type or Callable Interface.
1. Syntax for Hybrid Types
A hybrid type combines a Call Signature ((param: Type): ReturnType) with standard object property declarations inside an interface definition.
2. Real-World Use Case: Express Middleware & Axios Instance
Popular JavaScript libraries rely heavily on hybrid types:
axios('/api/user') is a callable function, but also has methods like axios.get(), axios.post(), and property axios.defaults.app(req, res) is a request listener function, but also has methods like app.use() and app.listen().Example: Typing an Axios-like Fetch Utility
3. Construct Signatures (new Operator)
Interfaces can also define Construct Signatures using the new keyword to type functions that act as constructor functions instantiated via new:
4. Interactive Code Playground
Test hybrid types below:
5. Common Pitfalls & Edge Cases
as Counter).6. Summary Table
| Signature Type | Syntax in Interface | Purpose |
|---|---|---|
| Call Signature | (args): ReturnType; | Types object invocation as a function fn() |
| Construct Signature | new (args): InstanceType; | Types object instantiation as constructor new Fn() |
propName: Type; | Types object property access fn.propName |