Custom declaration files help TypeScript understand code from JavaScript or third-party libraries without built-in types. They tell TypeScript what types and shapes to expect.
Writing custom declaration files in Typescript
declare module 'module-name' { export function functionName(param: type): returnType; export interface InterfaceName { property: type; } // other declarations }
Use declare module to describe external modules.
Use declare global to add global variables or types.
greet that takes a string and returns a string.declare module 'my-lib' { export function greet(name: string): string; }
myGlobalVar of type number to the global Window interface.declare global {
interface Window {
myGlobalVar: number;
}
}declare module 'awesome-tool' { export interface Options { verbose: boolean; } export function run(options: Options): void; }
This example shows a custom declaration file for a module 'math-utils' that exports two functions. Then the usage file imports and uses them with type safety.
// custom.d.ts declare module 'math-utils' { export function add(a: number, b: number): number; export function multiply(a: number, b: number): number; } // usage.ts import { add, multiply } from 'math-utils'; console.log(add(2, 3)); console.log(multiply(4, 5));
Declaration files usually have the extension .d.ts.
They only describe types and do not contain actual code implementations.
Place custom declaration files in a folder included in your tsconfig.json or reference them explicitly.
Custom declaration files tell TypeScript about types in JavaScript or untyped libraries.
Use declare module to describe modules and declare global for global types.
They help TypeScript check your code and improve editor support.
Practice
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 AQuick Check:
Custom declaration files = type info for JS [OK]
- Thinking declaration files speed up code execution
- Confusing declaration files with code transpilation
- Believing declaration files create new functions
Solution
Step 1: Recall correct module declaration syntax
TypeScript usesdeclare module 'name' { ... }to declare modules.Step 2: Check each option's syntax
Only declare module 'my-lib' { export function foo(): void; } uses the correct order and keywords.Final Answer:
declare module 'my-lib' { export function foo(): void; } -> Option BQuick Check:
Correct module syntax = declare module 'name' [OK]
- Swapping 'declare' and 'module' keywords
- Omitting quotes around module name
- Using incorrect keyword order
declare module 'utils' {
export function greet(name: string): string;
}What is the correct TypeScript output for
greet('Alice') if implemented as return `Hello, ${name}!`;?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 DQuick Check:
Function returns greeting string = Hello, Alice! [OK]
- Including quotes around string output incorrectly
- Confusing function call with output text
- Assuming runtime error without implementation
declare module 'math-lib' {
export function add(a: number, b: number): number
}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 AQuick Check:
Declaration statements need semicolons [OK]
- Forgetting semicolons in declaration files
- Thinking exports are not allowed
- Misnaming modules with wrong quotes
window.myAppVersion in a TypeScript project. Which custom declaration file snippet correctly adds this global property?Solution
Step 1: Understand how to add global types
Usedeclare globalblock 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 CQuick Check:
Global augmentation uses declare global + interface extension [OK]
- Trying to declare global types inside a module
- Omitting export {} to make file a module
- Declaring interface without 'declare global'
Ready to prove it?
Pass the test and this pattern moves unknown
