Using DefinitelyTyped and @types Packages in TypeScript
📖 Scenario: You are building a simple TypeScript project that uses a popular JavaScript library without built-in TypeScript types. To get type support, you will use the @types packages from DefinitelyTyped.
🎯 Goal: Learn how to install and use @types packages from DefinitelyTyped to add type definitions to JavaScript libraries in a TypeScript project.
📋 What You'll Learn
Create a TypeScript file that imports a JavaScript library
Install the corresponding @types package for that library
Use the library with type safety in TypeScript
Print a result using the library
💡 Why This Matters
🌍 Real World
Many JavaScript libraries do not include TypeScript types. Using @types packages from DefinitelyTyped lets you safely use these libraries in TypeScript projects.
💼 Career
Understanding how to add type definitions with @types packages is essential for working on TypeScript projects in professional development environments.
Progress0 / 4 steps
1
Create a TypeScript file importing lodash
Create a file named index.ts and write this line to import lodash: import _ from 'lodash';
Typescript
Hint
Lodash is a popular JavaScript utility library. We will import it first.
2
Install the @types/lodash package
Add a comment in index.ts that says: // @types package installed for lodash to indicate you installed @types/lodash in your project.
Typescript
Hint
In a real project, you would run npm install --save-dev @types/lodash. Here, just add the comment.
3
Use lodash to chunk an array with type safety
Write a line that creates a variable chunks by calling _.chunk on the array [1, 2, 3, 4] with chunk size 2: const chunks = _.chunk([1, 2, 3, 4], 2);
Typescript
Hint
The _.chunk function splits an array into smaller arrays of a given size.
4
Print the chunks variable
Write a line to print the chunks variable using console.log(chunks);
Typescript
Hint
Use console.log to see the output of the chunks variable.
Practice
(1/5)
1. What is the main purpose of the @types packages in TypeScript?
easy
A. To provide type definitions for JavaScript libraries
B. To compile TypeScript code into JavaScript
C. To run JavaScript code in the browser
D. To create new JavaScript libraries
Solution
Step 1: Understand the role of @types packages
@types packages contain type definitions that describe the shape and types of JavaScript libraries for TypeScript.
Step 2: Identify the main purpose
They help TypeScript understand JavaScript libraries by providing type information, enabling better type checking and editor support.
Final Answer:
To provide type definitions for JavaScript libraries -> Option A
Quick Check:
@types = type definitions [OK]
Hint: Remember: @types adds types for JS libs [OK]
Common Mistakes:
Confusing @types with compilers
Thinking @types runs code
Assuming @types creates libraries
2. Which of the following is the correct way to install type definitions for the lodash library using npm?
easy
A. npm install @types/lodash
B. npm install lodash-types
C. npm install types-lodash
D. npm install lodash/@types
Solution
Step 1: Recall the naming convention for type packages
Type definitions from DefinitelyTyped are published under the @types scope, so the package name is @types/library-name.
Step 2: Match the correct npm install command
For lodash, the correct package is @types/lodash, so the command is npm install @types/lodash.
Final Answer:
npm install @types/lodash -> Option A
Quick Check:
Type packages start with @types/ [OK]
Hint: Type packages always start with @types/ [OK]
Common Mistakes:
Using incorrect package names without @types/
Mixing up package and type package names
Trying to install types as normal packages
3. Given the following TypeScript code, what will be the output if @types/lodash is NOT installed?
import _ from 'lodash';
const numbers = [1, 2, 3];
const doubled = _.map(numbers, n => n * 2);
console.log(doubled);
medium
A. [] (empty array)
B. [2, 4, 6]
C. Runtime error: _.map is not a function
D. TypeScript error: Cannot find module 'lodash' or its corresponding type declarations
Solution
Step 1: Understand the role of type definitions in TypeScript
Without @types/lodash, TypeScript cannot find type information for lodash, causing a compile-time error.
Step 2: Identify the error message
The error will say it cannot find the module or its type declarations, preventing compilation.
Final Answer:
TypeScript error: Cannot find module 'lodash' or its corresponding type declarations -> Option D
Quick Check:
No types = compile error [OK]
Hint: No @types means TypeScript can't find types [OK]
Common Mistakes:
Confusing runtime errors with compile errors
Expecting code to run without types installed
Thinking JavaScript errors appear instead
4. You installed @types/express but still get errors about missing types when importing express. What is the most likely cause?
medium
A. You need to restart your TypeScript server or IDE
B. You installed the wrong version of @types/express
C. You forgot to install the express package itself
D. You must import types explicitly using import type
Solution
Step 1: Check dependencies
Type definitions only provide types; the actual library must be installed separately.
Step 2: Identify missing library
If express is not installed, importing it causes errors despite having types.
Final Answer:
You forgot to install the express package itself -> Option C
Quick Check:
Types need actual library installed [OK]
Hint: Always install both library and its @types [OK]
Common Mistakes:
Assuming types include the library code
Not restarting IDE after install
Misusing import type for libraries
5. You want to use a JavaScript library foo-lib in your TypeScript project, but no @types/foo-lib package exists. What is the best way to add type support?
hard
A. Ignore type errors and use any everywhere
B. Write a custom declaration file foo-lib.d.ts with needed types
C. Install @types/foo-lib anyway, it will work
D. Rewrite the library in TypeScript
Solution
Step 1: Understand missing type definitions
If no official @types package exists, you can create your own declaration file to describe the library's types.
Step 2: Choose best practice
Writing a .d.ts file lets TypeScript check your code and improves editor support without rewriting the library.
Final Answer:
Write a custom declaration file foo-lib.d.ts with needed types -> Option B
Quick Check:
Custom .d.ts files add missing types [OK]
Hint: Create .d.ts files for missing types [OK]
Common Mistakes:
Ignoring types and risking bugs
Expecting non-existent @types packages
Thinking rewriting library is easiest
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.