0
0
Typescriptprogramming~5 mins

Ambient declarations in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an ambient declaration in TypeScript?
An ambient declaration tells TypeScript about variables, functions, or types that exist elsewhere (like in JavaScript files or external libraries) without defining them in the current file.
Click to reveal answer
beginner
How do you declare an ambient variable in TypeScript?
Use the <code>declare</code> keyword before the variable name, for example: <pre>declare const myVar: string;</pre> This means <code>myVar</code> exists somewhere else.
Click to reveal answer
intermediate
Why use ambient declarations instead of normal declarations?
Ambient declarations avoid redefining or implementing code that already exists, helping TypeScript understand external code without duplication or errors.
Click to reveal answer
intermediate
What is the difference between declare var and declare module?
declare var tells TypeScript about a global variable, while declare module describes an external module or library's shape.
Click to reveal answer
beginner
Can ambient declarations include function signatures?
Yes! You can declare functions with declare function to tell TypeScript about functions defined elsewhere, including their parameters and return types.
Click to reveal answer
What keyword is used to create an ambient declaration in TypeScript?
Aexternal
Bdeclare
Cambient
Dimport
Which of the following is a correct ambient declaration for a global variable named apiKey of type string?
Aconst apiKey: string;
Bvar apiKey: string;
Cdeclare const apiKey: string;
Ddeclare apiKey = '123';
What does an ambient module declaration describe?
AAn external module's shape
BA local variable
CA function implementation
DA class instance
Can ambient declarations include implementations (code bodies)?
ANo, they only describe types and signatures
BYes, always
COnly for variables
DOnly for functions
Where are ambient declarations commonly used?
ATo create CSS styles
BTo write application logic
CTo declare local variables
DTo describe external JavaScript libraries
Explain what ambient declarations are and why they are useful in TypeScript.
Think about how TypeScript knows about code written outside your files.
You got /4 concepts.
    Write an example of an ambient declaration for a function named fetchData that takes a string and returns a Promise of any.
    Use the declare keyword and only write the function signature.
    You got /4 concepts.