0
0
Typescriptprogramming~20 mins

DefinitelyTyped and @types packages in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DefinitelyTyped Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when using a missing @types package?

Consider this TypeScript code that imports a library without an installed @types package. What will the TypeScript compiler report?

Typescript
import _ from 'lodash';

const result = _.chunk([1, 2, 3, 4], 2);
console.log(result);
ACompilation warning only, but code runs fine.
BCompilation error: Cannot find module 'lodash' or its corresponding type declarations.
CRuntime error: '_' is undefined.
DNo compilation error; the code compiles and runs, but TypeScript treats '_' as 'any' type.
Attempts:
2 left
💡 Hint

Think about what happens if TypeScript has no type info but the package is installed.

🧠 Conceptual
intermediate
1:30remaining
Why use the DefinitelyTyped repository?

What is the main purpose of the DefinitelyTyped repository in the TypeScript ecosystem?

ATo host community-maintained type definitions for JavaScript libraries that lack built-in TypeScript types.
BTo compile TypeScript code into JavaScript automatically.
CTo provide official runtime implementations of popular JavaScript libraries.
DTo manage package versions for npm.
Attempts:
2 left
💡 Hint

Think about what type definitions are and who maintains them when libraries don't provide their own.

🔧 Debug
advanced
2:00remaining
Identify the error caused by missing @types package

Given this TypeScript code snippet, what error will the compiler show if @types/express is not installed?

Typescript
import express from 'express';

const app = express();
app.get('/', (req, res) => {
  res.send('Hello World');
});
AError: Cannot find module 'express' or its corresponding type declarations.
BError: Property 'get' does not exist on type 'any'.
CNo error; code compiles and runs fine.
DError: Cannot find name 'express'.
Attempts:
2 left
💡 Hint

Consider what happens when the type declarations for a module are missing.

📝 Syntax
advanced
1:30remaining
Which import syntax correctly uses @types package types?

Assuming @types/moment is installed, which import statement correctly imports the moment library with types?

Aimport { moment } from 'moment';
Bimport * as moment from 'moment';
Cconst moment = require('moment');
Dimport moment from 'moment';
Attempts:
2 left
💡 Hint

Check the recommended import style for ES modules with default export.

🚀 Application
expert
3:00remaining
How to add types for a JavaScript library without existing @types package?

You want to use a JavaScript library foo-lib in your TypeScript project, but there is no @types/foo-lib package available. What is the best way to add type safety?

AUse <code>declare module 'foo-lib';</code> in a global .ts file without any type details.
BIgnore type errors and use <code>any</code> type everywhere for <code>foo-lib</code> imports.
CCreate a <code>foo-lib.d.ts</code> file with minimal type declarations and include it in your project.
DFork DefinitelyTyped and submit a pull request with full type definitions before using the library.
Attempts:
2 left
💡 Hint

Think about a practical way to get some type safety quickly.