TypeScript DefinitelyTyped
📦 TypeScript DefinitelyTyped (@types)
👉 These type definitions are published on npm under the @types scope.
🔍 Why DefinitelyTyped is Need
Many popular JavaScript libraries (like jQuery, Lodash, Express) were originally written in plain JavaScript, so they don’t include TypeScript types.
Without types ❌:
-
No auto-completion
-
No type checking
-
More runtime errors
With DefinitelyTyped ✅:
-
Full IntelliSense
-
Compile-time safety
-
Better developer experience
🧠 What are Type Definitions?
Type definitions describe:
-
Functions
-
Objects
-
Parameters
-
Return types
They usually come in .d.ts files.
Example:
📥 Installing Type Definitions
Use npm to install types.
Example: jQuery
Now TypeScript understands jQuery:
📦 Common @types Packages
| Library | Types Package |
|---|---|
| Express | @types/express |
| Node.js | @types/node |
| React | @types/react |
| Lodash | @types/lodash |
| jQuery | @types/jquery |
⚙️ How TypeScript Finds @types
TypeScript automatically looks in:
You don’t need to import them manually in most cases.
🧪 Example with Express
✔ req and res are now fully typed
🔧 When You DON’T Need DefinitelyTyped
You don’t need @types if:
-
The library writes in TypeScript
-
The library includes its own
.d.tsfiles
Example:
Axios already includes types ✅
📝 Custom Type Declarations
If no @types package exists, you can write your own.
🔑 DefinitelyTyped Summary
| Concept | Meaning |
|---|---|
| DefinitelyTyped | Repo of type definitions |
@types/* |
npm packages for types |
.d.ts |
Type declaration files |
| Benefit | Type safety for JS libs |
⭐ Best Practices
✔ Always install @types for JS libraries
✔ Add types as devDependencies
✔ Avoid any by using proper typings
✔ Write custom .d.ts if needed
