0
0
Typescriptprogramming~5 mins

Writing custom declaration files in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a custom declaration file in TypeScript?
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
Which keyword is used to declare a module in a custom declaration file?
Aimport
Bmodule
Cexport
Ddeclare
What does the 'declare' keyword do in a declaration file?
ADescribes types without generating code
BExports variables
CImports modules
DGenerates JavaScript code
How do you specify a function's return type in a declaration file?
Afunction foo(): void {}
Bfunction foo() => number;
Cfunction foo(): number;
Dfunction foo() {}
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
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.