Module Augmentation: Adding Types to External Libraries
Module augmentation is like monkey-patching for types, letting you add definitions to external modules. Use it to add a `user` property to Express's `Request` object.
Module augmentation is like monkey-patching for types. It allows you to safely add your own type definitions to external modules you don't control, making TypeScript aware of your custom extensions. For example, you can augment Express's `Request` type to include a `user` property after authentication middleware runs. The main footgun is getting the syntax wrong: you must wrap your extension in a `declare module 'module-name'` block. Without it, TypeScript thinks you're creating a brand new module, and the augmentation won't apply.
Read the original → typescriptlang.org
- #typescript
- #modules
- #types
- #declaration merging
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.