0
0
Typescriptprogramming~20 mins

Ambient declarations in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ambient Declaration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this ambient declaration usage?

Consider the following TypeScript code using an ambient declaration. What will be the output when running the compiled JavaScript?

Typescript
declare const greeting: string;
console.log(greeting);
AReferenceError: greeting is not defined
Bundefined
CThe string value of greeting if defined globally
DCompilation error due to missing initialization
Attempts:
2 left
💡 Hint

Ambient declarations tell TypeScript about variables that exist elsewhere but do not create them.

🧠 Conceptual
intermediate
1:30remaining
What is the purpose of ambient declarations in TypeScript?

Why do developers use ambient declarations in TypeScript?

ATo enforce strict typing on all variables in the project
BTo create new variables with initial values in TypeScript files
CTo declare variables or types that exist in the global scope or external scripts without defining them
DTo automatically import external modules into the project
Attempts:
2 left
💡 Hint

Think about how TypeScript knows about variables from other scripts or libraries.

Predict Output
advanced
2:00remaining
What error does this ambient module declaration cause?

Examine this TypeScript code snippet. What error will it produce?

Typescript
declare module 'myLib' {
  export function greet(name: string): string;
}

import { greet } from 'myLib';
console.log(greet(123));
ANo error, outputs '123'.
BCannot find module 'myLib'.
CRuntime error: greet is not a function.
DType 'number' is not assignable to type 'string'.
Attempts:
2 left
💡 Hint

Check the argument type passed to the function versus the declared type.

🔧 Debug
advanced
2:00remaining
Why does this ambient variable cause a runtime error?

Given this ambient declaration and usage, why does the code fail at runtime?

Typescript
declare var config: { apiKey: string };
console.log(config.apiKey);
ABecause <code>config</code> is declared but not defined anywhere in JavaScript at runtime
BBecause <code>apiKey</code> is not a string
CBecause <code>declare var</code> cannot be used with objects
DBecause TypeScript does not allow accessing properties on ambient variables
Attempts:
2 left
💡 Hint

Ambient declarations do not create variables in the output JavaScript.

🚀 Application
expert
2:30remaining
How many properties does the ambient interface add to the global Window object?

Given this ambient declaration extending the global Window interface, how many new properties are added to window?

Typescript
declare global {
  interface Window {
    appVersion: string;
    isLoggedIn: boolean;
    logout(): void;
  }
}

console.log(Object.keys(window).includes('appVersion'));
A3 properties: <code>appVersion</code>, <code>isLoggedIn</code>, and <code>logout</code>
B0 properties, because ambient declarations do not add properties at runtime
C1 property: <code>appVersion</code> only
D2 properties: <code>appVersion</code> and <code>isLoggedIn</code> only
Attempts:
2 left
💡 Hint

Ambient declarations describe types but do not create actual properties in JavaScript.