A custom declaration file (.d.ts) tells TypeScript about the types of code written in JavaScript or external libraries without type definitions. It helps TypeScript understand the shape and types of those modules.
Click to reveal answer
beginner
How do you declare a module in a custom declaration file?
Use the declare module 'module-name' { ... } syntax to describe the types and exports of that module inside the curly braces.
Click to reveal answer
beginner
Why use declare keyword in declaration files?
The declare keyword tells TypeScript that the code exists elsewhere (like JavaScript code) and only describes its shape or types without generating JavaScript code.
Click to reveal answer
intermediate
How to declare a function type in a custom declaration file?
Inside the declaration file, write the function signature like <code>function myFunc(param: string): number;</code> inside a module or global scope to describe its parameters and return type.
Click to reveal answer
beginner
What is the purpose of export in declaration files?
Using <code>export</code> inside a declaration file makes the declared types, interfaces, or functions available to other files that import the module.
Click to reveal answer
What file extension is used for TypeScript declaration files?
A.ts
B.d.ts
C.js
D.json
✗ Incorrect
Declaration files always use the .d.ts extension to tell TypeScript about types without generating JavaScript.
Which keyword is used to declare a module in a custom declaration file?
Aimport
Bmodule
Cexport
Ddeclare
✗ Incorrect
The 'declare' keyword is used to declare modules and other types in declaration files.
What does the 'declare' keyword do in a declaration file?
ADescribes types without generating code
BExports variables
CImports modules
DGenerates JavaScript code
✗ Incorrect
'declare' tells TypeScript about existing code's types without creating JavaScript output.
How do you specify a function's return type in a declaration file?
Afunction foo(): void {}
Bfunction foo() => number;
Cfunction foo(): number;
Dfunction foo() {}
✗ Incorrect
In declaration files, function signatures end with a semicolon and specify return type after colon.
Why add 'export' inside a declaration file?
ATo make types available for import
BTo hide types from other files
CTo run code at runtime
DTo import other modules
✗ Incorrect
'export' makes declared types or functions accessible to other files importing the module.
Explain how to write a custom declaration file for a JavaScript module without types.
Think about how to tell TypeScript about a JS module's shape.
You got /5 concepts.
Describe the role of the 'declare' keyword in TypeScript declaration files.
It tells TypeScript about something without creating code.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of writing a custom declaration file in TypeScript?
easy
A. To tell TypeScript about types in JavaScript or untyped libraries
B. To write JavaScript code that runs faster
C. To convert TypeScript code into JavaScript
D. To create new JavaScript functions
Solution
Step 1: Understand the role of declaration files
Declaration files provide type information about JavaScript code or libraries without types.
Step 2: Identify the purpose in TypeScript
This helps TypeScript check code and improve editor support by knowing types.
Final Answer:
To tell TypeScript about types in JavaScript or untyped libraries -> Option A
Quick Check:
Custom declaration files = type info for JS [OK]
Hint: Declaration files describe types for untyped JS code [OK]
Common Mistakes:
Thinking declaration files speed up code execution
Confusing declaration files with code transpilation
Believing declaration files create new functions
2. Which syntax correctly declares a module named 'my-lib' in a custom declaration file?
easy
A. module declare 'my-lib' { function foo(): void; }
B. declare module 'my-lib' { export function foo(): void; }
C. declare 'my-lib' module { export function foo(): void; }
D. module 'my-lib' declare { export function foo(): void; }
Hint: Use 'declare module' followed by module name in quotes [OK]
Common Mistakes:
Swapping 'declare' and 'module' keywords
Omitting quotes around module name
Using incorrect keyword order
3. Given this declaration file snippet:
declare module 'utils' {
export function greet(name: string): string;
}
What is the correct TypeScript output for greet('Alice') if implemented as return `Hello, ${name}!`;?
medium
A. "Hello, Alice!"
B. Error: greet is not defined
C. greet(Alice)
D. Hello, Alice!
Solution
Step 1: Understand the declaration and implementation
The declaration says greet takes a string and returns a string. The implementation returns a template string greeting.
Step 2: Evaluate the output of greet('Alice')
Calling greet('Alice') returns the string Hello, Alice! without quotes in the output.
Final Answer:
Hello, Alice! -> Option D
Quick Check:
Function returns greeting string = Hello, Alice! [OK]
Hint: String return does not include quotes in output display [OK]
Common Mistakes:
Including quotes around string output incorrectly
Confusing function call with output text
Assuming runtime error without implementation
4. Identify the error in this custom declaration file snippet:
declare module 'math-lib' {
export function add(a: number, b: number): number
}
medium
A. Missing semicolon after function declaration
B. Wrong module name syntax
C. Function parameters must be optional
D. Cannot export functions in declaration files
Solution
Step 1: Check syntax inside module declaration
Each statement inside declaration files must end with a semicolon.
Step 2: Identify missing semicolon
The function declaration line lacks a semicolon at the end.
Final Answer:
Missing semicolon after function declaration -> Option A
Quick Check:
Declaration statements need semicolons [OK]
Hint: Always end declarations with semicolons [OK]
Common Mistakes:
Forgetting semicolons in declaration files
Thinking exports are not allowed
Misnaming modules with wrong quotes
5. You want to add a global type for a new property window.myAppVersion in a TypeScript project. Which custom declaration file snippet correctly adds this global property?
B. declare module 'window' { export const myAppVersion: string; }
C. declare global { interface Window { myAppVersion: string; } } export {};
D. interface Window { myAppVersion: string; }
Solution
Step 1: Understand how to add global types
Use declare global block to augment global interfaces like Window.
Step 2: Check each option for correct syntax
declare global { interface Window { myAppVersion: string; } } export {}; correctly declares global and extends Window interface, then exports empty to make it a module.
Final Answer:
declare global { interface Window { myAppVersion: string; } } export {}; -> Option C
Quick Check:
Global augmentation uses declare global + interface extension [OK]
Hint: Use 'declare global' to add global types safely [OK]
Common Mistakes:
Trying to declare global types inside a module
Omitting export {} to make file a module
Declaring interface without 'declare global'
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.