Bird
Raised Fist0
Typescriptprogramming~20 mins

Strict configuration objects in Typescript - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1.

What is the main benefit of using strict configuration objects in TypeScript?

easy
A. They allow any keys to be added without errors.
B. They make the code run faster at runtime.
C. They catch errors early by enforcing exact keys and types.
D. They automatically generate UI forms for settings.

Solution

  1. Step 1: Understand strict configuration objects

    Strict configs define exactly which keys and types are allowed in an object.
  2. Step 2: Identify the main benefit

    This strictness helps catch mistakes early, before running the code.
  3. Final Answer:

    They catch errors early by enforcing exact keys and types. -> Option C
  4. Quick Check:

    Strict config = early error detection [OK]
Hint: Strict configs prevent wrong keys and types early [OK]
Common Mistakes:
  • Thinking strict configs improve runtime speed
  • Believing strict configs allow any keys
  • Confusing strict configs with UI generation
2.

Which of the following is the correct way to declare a strict configuration object type in TypeScript?

interface Config {
host: string;
port: number;
}
easy
A. const config: Config = { host: 'localhost', port: 8080 };
B. const config: Config = { host: 'localhost' };
C. const config: Config = { host: 'localhost', port: '8080' };
D. const config = { host: 'localhost', port: 8080 };

Solution

  1. Step 1: Check interface requirements

    The Config interface requires both 'host' as string and 'port' as number.
  2. Step 2: Match object to interface

    const config: Config = { host: 'localhost', port: 8080 }; has both keys with correct types, so it matches Config exactly.
  3. Final Answer:

    const config: Config = { host: 'localhost', port: 8080 }; -> Option A
  4. Quick Check:

    All required keys with correct types = const config: Config = { host: 'localhost', port: 8080 }; [OK]
Hint: Match all keys and types exactly to interface [OK]
Common Mistakes:
  • Omitting required keys
  • Using wrong types for keys
  • Not specifying the type annotation
3.

What will be the output of this TypeScript code?

interface Settings {
debug: boolean;
version: string;
}

const config: Settings = { debug: true, version: '1.0' };
console.log(config.debug, config.version);
medium
A. true 1.0
B. true '1.0'
C. undefined undefined
D. Error: Missing properties

Solution

  1. Step 1: Check object matches interface

    The object has 'debug' as true (boolean) and 'version' as '1.0' (string), matching Settings.
  2. Step 2: Understand console.log output

    console.log prints the boolean true and string '1.0' without quotes, so output is: true 1.0
  3. Final Answer:

    true 1.0 -> Option A
  4. Quick Check:

    Matching keys print their values correctly [OK]
Hint: Check object keys match interface and print values directly [OK]
Common Mistakes:
  • Expecting quotes around string in console output
  • Thinking missing keys cause runtime error
  • Confusing type errors with runtime output
4.

Identify the error in this TypeScript code using a strict configuration object:

interface Options {
timeout: number;
verbose: boolean;
}

const opts: Options = { timeout: 5000, verbose: 'yes' };
medium
A. Property 'timeout' is missing.
B. Property 'verbose' should be a boolean, not a string.
C. Extra property 'verbose' is not allowed.
D. No error, code is correct.

Solution

  1. Step 1: Check property types against interface

    'timeout' is number 5000, correct. 'verbose' is string 'yes', but interface requires boolean.
  2. Step 2: Identify type mismatch error

    Assigning string to boolean property causes a type error.
  3. Final Answer:

    Property 'verbose' should be a boolean, not a string. -> Option B
  4. Quick Check:

    Type mismatch on 'verbose' property [OK]
Hint: Check each property type matches interface exactly [OK]
Common Mistakes:
  • Ignoring type mismatch errors
  • Thinking missing properties cause this error
  • Confusing extra properties with wrong types
5.

You want to create a strict configuration object that only allows keys mode (string), retry (number), and enabled (boolean). Which TypeScript type definition ensures no extra keys are allowed?

hard
A. interface Config { mode: string; retry: number; enabled: boolean; }
B. type Config = { mode: string; retry: number; enabled: boolean; [key: string]: any }
C. type Config = { mode?: string; retry?: number; enabled?: boolean }
D. interface Config { mode: string; retry: number; enabled: boolean; [key: string]: never }

Solution

  1. Step 1: Understand how to forbid extra keys

    Using an index signature with [key: string]: never forbids any keys not explicitly declared.
  2. Step 2: Compare options

    interface Config { mode: string; retry: number; enabled: boolean; } allows extra keys implicitly. type Config = { mode: string; retry: number; enabled: boolean; [key: string]: any } allows any extra keys. type Config = { mode?: string; retry?: number; enabled?: boolean } makes keys optional but doesn't forbid extras. Only interface Config { mode: string; retry: number; enabled: boolean; [key: string]: never } forbids extra keys.
  3. Final Answer:

    interface Config { mode: string; retry: number; enabled: boolean; [key: string]: never } -> Option D
  4. Quick Check:

    Index signature with never forbids extra keys [OK]
Hint: Use [key: string]: never to forbid extra keys [OK]
Common Mistakes:
  • Thinking interface alone forbids extra keys
  • Using any type index signature allowing extras
  • Making keys optional but not forbidding extras
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard