0
0
Typescriptprogramming~20 mins

Why declaration files are needed in Typescript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Declaration Files Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Purpose of Declaration Files in TypeScript

Why do we need declaration files (.d.ts) in TypeScript projects?

AThey compile TypeScript code into JavaScript automatically.
BThey provide type information for JavaScript libraries so TypeScript can check types correctly.
CThey store runtime values used by TypeScript programs.
DThey are used to write CSS styles for TypeScript components.
Attempts:
2 left
💡 Hint

Think about how TypeScript understands code from plain JavaScript libraries.

Predict Output
intermediate
2:00remaining
Output when using a library without declaration files

What happens when you try to use a JavaScript library in TypeScript without a declaration file?

import { foo } from 'some-js-lib';

foo();
ATypeScript shows an error: Cannot find module 'some-js-lib' or its corresponding type declarations.
BThe code runs perfectly without any errors or warnings.
CTypeScript automatically generates types and compiles without errors.
DThe program crashes at runtime with a ReferenceError.
Attempts:
2 left
💡 Hint

TypeScript needs type info to understand external code.

🔧 Debug
advanced
2:00remaining
Fixing missing declaration file error

Given this error in a TypeScript project:

error TS7016: Could not find a declaration file for module 'my-lib'.

Which option correctly fixes this error?

AAdd <code>"allowJs": false</code> in <code>tsconfig.json</code>.
BRename all <code>.ts</code> files to <code>.js</code> files.
CInstall the type declarations package with <code>npm install --save-dev @types/my-lib</code>.
DDelete the <code>tsconfig.json</code> file to disable type checking.
Attempts:
2 left
💡 Hint

Look for official type packages on npm.

📝 Syntax
advanced
2:00remaining
Correct syntax for a declaration file

Which of the following is a correct way to declare a function in a TypeScript declaration file (.d.ts)?

Aconst greet = (name: string) => { return name; };
Bfunction greet(name: string) { console.log(name); }
Cexport function greet(name) { return name; }
Ddeclare function greet(name: string): void;
Attempts:
2 left
💡 Hint

Declaration files only describe types, no function bodies.

🚀 Application
expert
3:00remaining
Using declaration files to integrate a JavaScript library

You want to use a JavaScript library math-tools in your TypeScript project, but it has no built-in types. You create a math-tools.d.ts file with the following content:

declare module 'math-tools' {
  export function add(a: number, b: number): number;
  export function multiply(a: number, b: number): number;
}

What is the effect of adding this declaration file?

ATypeScript can now check types when you import and use 'math-tools' functions.
BThe JavaScript code of 'math-tools' is replaced by the declaration file at runtime.
CYou must rewrite all 'math-tools' functions in TypeScript for this to work.
DThe declaration file automatically compiles 'math-tools' to TypeScript.
Attempts:
2 left
💡 Hint

Declaration files provide type info but do not change runtime code.