0
0
Typescriptprogramming~20 mins

Strict configuration objects in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Strict Config Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a strict config object with excess property check

What is the output of this TypeScript code when compiled and run with strict mode enabled?

Typescript
interface Config {
  url: string;
  timeout: number;
}

const config: Config = {
  url: "https://example.com",
  timeout: 5000,
  debug: true
};

console.log(config.timeout);
ACompilation error due to excess property 'debug'
B5000
Cundefined
DRuntime error: Property 'debug' does not exist
Attempts:
2 left
💡 Hint

Think about how TypeScript's strict object literal checks work when assigning to a typed variable.

🧠 Conceptual
intermediate
2:00remaining
Understanding readonly properties in strict config objects

Given this TypeScript interface and object, which statement is true about modifying the config object?

interface Config {
  readonly apiKey: string;
  retries: number;
}

const config: Config = { apiKey: "abc123", retries: 3 };
config.retries = 5;
config.apiKey = "xyz789";
ABoth assignments cause errors
BAssignment to 'retries' is allowed; assignment to 'apiKey' causes an error
CAssignment to 'apiKey' is allowed; assignment to 'retries' causes an error
DBoth assignments are allowed without error
Attempts:
2 left
💡 Hint

Consider what the readonly keyword means in TypeScript.

🔧 Debug
advanced
2:00remaining
Why does this strict config object cause a type error?

Identify the cause of the TypeScript error in this code snippet:

interface Config {
  host: string;
  port: number;
  secure?: boolean;
}

const config: Config = {
  host: "localhost",
  port: 8080,
  secure: "true"
};
AOptional properties cannot be assigned any value
BThe property 'port' should be a string, not a number
CThe property 'host' is missing
DThe property 'secure' is optional but assigned a string instead of boolean
Attempts:
2 left
💡 Hint

Check the type of the optional property and the assigned value.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a strict config object with nested readonly properties?

Choose the option that correctly defines a TypeScript interface with a nested readonly config object and an instance that respects strict typing.

A
interface Config { readonly server: { readonly host: string; readonly port: number } }
const config: Config = { server: { host: "127.0.0.1", port: 80 } };
config.server.port = 8080;
B
interface Config { server: { readonly host: string; readonly port: number } }
const config: Config = { server: { host: "127.0.0.1", port: 80 } };
config.server.port = 8080;
C
interface Config { readonly server: { host: string; port: number } }
const config: Config = { server: { host: "127.0.0.1", port: 80 } };
config.server.port = 8080;
D
interface Config { server: { host: string; port: number } }
const config: Config = { server: { host: "127.0.0.1", port: 80 } };
config.server.port = 8080;
Attempts:
2 left
💡 Hint

Consider where the readonly modifier applies and what it protects.

🚀 Application
expert
2:00remaining
How many keys are in the resulting strict config object?

Given this TypeScript code, how many keys does the finalConfig object have at runtime?

interface BaseConfig {
  host: string;
  port: number;
}

interface ExtendedConfig extends BaseConfig {
  secure: boolean;
  timeout?: number;
}

const base: BaseConfig = { host: "localhost", port: 3000 };
const extra = { secure: true, timeout: 1000 };

const finalConfig: ExtendedConfig = { ...base, ...extra };

console.log(Object.keys(finalConfig).length);
A2
B3
C4
D5
Attempts:
2 left
💡 Hint

Think about how spreading objects merges keys and which keys exist in the final object.