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
✗ Incorrect
The declare keyword is used to tell TypeScript about variables or functions that exist elsewhere.
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';
✗ Incorrect
Ambient declarations use declare without assigning a value.
What does an ambient module declaration describe?
AAn external module's shape
BA local variable
CA function implementation
DA class instance
✗ Incorrect
Ambient module declarations describe external modules or libraries.
Can ambient declarations include implementations (code bodies)?
ANo, they only describe types and signatures
BYes, always
COnly for variables
DOnly for functions
✗ Incorrect
Ambient declarations only describe the shape, not the implementation.
Where are ambient declarations commonly used?
ATo create CSS styles
BTo write application logic
CTo declare local variables
DTo describe external JavaScript libraries
✗ Incorrect
They help TypeScript understand external JavaScript code.
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.
Practice
(1/5)
1. What is the main purpose of an ambient declaration in TypeScript?
easy
A. To tell TypeScript about code that exists elsewhere without generating JavaScript
B. To create new variables that TypeScript will compile into JavaScript
C. To define functions that run only during development
D. To import modules from npm packages automatically
Solution
Step 1: Understand what ambient declarations do
Ambient declarations use the declare keyword to describe code that is already present, like global variables or external libraries.
Step 2: Recognize that ambient declarations do not produce JavaScript
They only help TypeScript check types and do not generate any JavaScript code.
Final Answer:
To tell TypeScript about code that exists elsewhere without generating JavaScript -> Option A
Hint: Remember: declare means 'exists elsewhere' in TypeScript [OK]
Common Mistakes:
Thinking declare creates new code in output
Confusing ambient declarations with imports
Assuming declare runs code at runtime
2. Which of the following is the correct syntax for declaring an ambient variable named API_KEY of type string?
easy
A. let API_KEY: string = '123';
B. var API_KEY: string;
C. declare var API_KEY: string;
D. const API_KEY = '123';
Solution
Step 1: Identify ambient declaration syntax
Ambient declarations use the declare keyword before the variable declaration to tell TypeScript it exists elsewhere.
Step 2: Check the options for correct usage
declare var API_KEY: string; uses declare var API_KEY: string; which is the correct ambient declaration syntax without initialization.
Final Answer:
declare var API_KEY: string; -> Option C
Quick Check:
Ambient var declaration = declare var [OK]
Hint: Ambient vars always use 'declare' without initialization [OK]
Common Mistakes:
Initializing variables in ambient declarations
Omitting the declare keyword
Using let or const with declare incorrectly
3. Given the ambient declaration below, what will be the output of the following code?
declare function greet(name: string): void;
greet('Alice');
medium
A. Hello, Alice!
B. No output, because greet is only declared
C. TypeScript compile error
D. Error: greet is not defined
Solution
Step 1: Understand what declare does for functions
The declare keyword tells TypeScript that the function greet exists elsewhere, but it does not provide an implementation.
Step 2: Consider runtime behavior
Since no actual JavaScript function greet is defined, calling greet('Alice') will cause a runtime error: greet is not defined.
Final Answer:
Error: greet is not defined -> Option D
Quick Check:
declare function = no runtime code [OK]
Hint: declare only types, no runtime code created [OK]
Common Mistakes:
Assuming declare creates function implementation
Expecting output without defining function
Confusing compile-time and runtime behavior
4. Identify the error in this ambient declaration and fix it:
declare const version = '1.0.0';
medium
A. Ambient declarations cannot have initializers; remove '= '1.0.0''
B. Change 'const' to 'let' to allow initialization
C. Add type annotation: declare const version: string = '1.0.0';
D. No error; code is correct
Solution
Step 1: Recall ambient declaration rules for variables
Ambient declarations cannot include initializers because they only describe existing code.
Step 2: Fix the code by removing the initializer
Remove the = '1.0.0' part, leaving declare const version: string; to fix the error.
Final Answer:
Ambient declarations cannot have initializers; remove '= '1.0.0'' -> Option A
Quick Check:
No initializers in declare vars [OK]
Hint: No value assignments in declare statements [OK]
Common Mistakes:
Adding initial values in declare statements
Changing const to let without removing initializer
Ignoring type annotations in ambient declarations
5. You want to use a global JavaScript library that defines a global variable Lib with a method init(). How should you write an ambient declaration to use it safely in TypeScript?
hard
A. const Lib = { init(): void {} };
B. declare const Lib: { init(): void; };
C. declare function Lib(): void;
D. interface Lib { init(): void; }
Solution
Step 1: Understand the global variable structure
The global variable Lib is an object with a method init(), so it should be declared as a constant with a method signature.
Step 2: Write the correct ambient declaration
Use declare const Lib: { init(): void; }; to tell TypeScript about the existing global object and its method without implementation.
Final Answer:
declare const Lib: { init(): void; }; -> Option B
Quick Check:
Declare global object with method signatures [OK]
Hint: Use declare const with method signatures for global objects [OK]
Common Mistakes:
Using interface instead of declare const for global vars