What is the output of this TypeScript code when compiled and run with strict mode enabled?
interface Config {
url: string;
timeout: number;
}
const config: Config = {
url: "https://example.com",
timeout: 5000,
debug: true
};
console.log(config.timeout);Think about how TypeScript's strict object literal checks work when assigning to a typed variable.
In strict mode, TypeScript does not allow extra properties that are not defined in the interface when assigning an object literal directly. The property 'debug' is not in Config, so this causes a compilation error.
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";Consider what the readonly keyword means in TypeScript.
The readonly modifier means the property cannot be changed after initialization. So apiKey cannot be reassigned, but retries can.
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"
};Check the type of the optional property and the assigned value.
The 'secure' property is optional and expects a boolean if provided. Assigning a string '"true"' causes a type mismatch error.
Choose the option that correctly defines a TypeScript interface with a nested readonly config object and an instance that respects strict typing.
Consider where the readonly modifier applies and what it protects.
Option A correctly marks the nested properties as readonly, so attempts to modify them cause errors. The outer object is readonly, and nested properties are also readonly, enforcing strict immutability.
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);Think about how spreading objects merges keys and which keys exist in the final object.
The finalConfig object has keys: 'host', 'port', 'secure', and 'timeout'. So the length of keys is 4.