Strict configuration objects help catch mistakes early by making sure only allowed settings are used.
Strict configuration objects in Typescript
function setup(config: { optionA: string; optionB: number }): void { // use config.optionA and config.optionB }
Define the exact shape of the config object using an inline type or an interface.
TypeScript will check that only the specified keys are present.
debug and maxRetries keys.function configure(settings: { debug: boolean; maxRetries: number }) { console.log(settings.debug, settings.maxRetries); }
interface Config {
url: string;
timeout: number;
}
function connect(config: Config) {
console.log(`Connecting to ${config.url} with timeout ${config.timeout}`);
}function start(options: { verbose: boolean }) { console.log(options.verbose ? 'Verbose mode on' : 'Verbose mode off'); } start({ verbose: true });
This program defines a strict config object for server settings and prints the server address.
interface ServerConfig {
host: string;
port: number;
}
function startServer(config: ServerConfig) {
console.log(`Server running at http://${config.host}:${config.port}`);
}
startServer({ host: 'localhost', port: 8080 });If you pass extra keys not defined in the config type, TypeScript will show an error.
Strict config objects help prevent bugs caused by misspelled or unexpected properties.
Strict configuration objects define exactly what settings are allowed.
They help catch errors early by checking object keys and types.
Use interfaces or inline types to create strict configs in TypeScript.
Practice
What is the main benefit of using strict configuration objects in TypeScript?
Solution
Step 1: Understand strict configuration objects
Strict configs define exactly which keys and types are allowed in an object.Step 2: Identify the main benefit
This strictness helps catch mistakes early, before running the code.Final Answer:
They catch errors early by enforcing exact keys and types. -> Option CQuick Check:
Strict config = early error detection [OK]
- Thinking strict configs improve runtime speed
- Believing strict configs allow any keys
- Confusing strict configs with UI generation
Which of the following is the correct way to declare a strict configuration object type in TypeScript?
interface Config {
host: string;
port: number;
}Solution
Step 1: Check interface requirements
The Config interface requires both 'host' as string and 'port' as number.Step 2: Match object to interface
const config: Config = { host: 'localhost', port: 8080 }; has both keys with correct types, so it matches Config exactly.Final Answer:
const config: Config = { host: 'localhost', port: 8080 }; -> Option AQuick Check:
All required keys with correct types = const config: Config = { host: 'localhost', port: 8080 }; [OK]
- Omitting required keys
- Using wrong types for keys
- Not specifying the type annotation
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);Solution
Step 1: Check object matches interface
The object has 'debug' as true (boolean) and 'version' as '1.0' (string), matching Settings.Step 2: Understand console.log output
console.log prints the boolean true and string '1.0' without quotes, so output is: true 1.0Final Answer:
true 1.0 -> Option AQuick Check:
Matching keys print their values correctly [OK]
- Expecting quotes around string in console output
- Thinking missing keys cause runtime error
- Confusing type errors with runtime output
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' };Solution
Step 1: Check property types against interface
'timeout' is number 5000, correct. 'verbose' is string 'yes', but interface requires boolean.Step 2: Identify type mismatch error
Assigning string to boolean property causes a type error.Final Answer:
Property 'verbose' should be a boolean, not a string. -> Option BQuick Check:
Type mismatch on 'verbose' property [OK]
- Ignoring type mismatch errors
- Thinking missing properties cause this error
- Confusing extra properties with wrong types
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?
Solution
Step 1: Understand how to forbid extra keys
Using an index signature with[key: string]: neverforbids any keys not explicitly declared.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.Final Answer:
interface Config { mode: string; retry: number; enabled: boolean; [key: string]: never } -> Option DQuick Check:
Index signature with never forbids extra keys [OK]
- Thinking interface alone forbids extra keys
- Using any type index signature allowing extras
- Making keys optional but not forbidding extras
Ready to prove it?
Pass the test and this pattern moves unknown
