Declaration Merging Grows Config Types
Two interface declarations with the same name merge into one: fields from every declaration become required together and keyof unions reflect the full set.
Code
interface PipelineConfig {
name: string;
retries: number;
}
// 同名 interface 自动合并:不同模块可以各自补充字段
interface PipelineConfig {
timeoutMs: number;
verbose?: boolean;
}
// 合并后的类型要求全部必填字段齐全(缺一个都过不了编译)
const config: PipelineConfig = {
name: 'nightly-sync',
retries: 2,
timeoutMs: 30000,
verbose: true,
};
// 键名联合也是合并后的全集
type ConfigKeys = keyof PipelineConfig;
const keys: ConfigKeys[] = ['name', 'retries', 'timeoutMs', 'verbose'];
console.log(keys.join(', '));
console.log(`${config.name}: retries=${config.retries} timeout=${config.timeoutMs}ms`);Output
Captured from a sandboxed run of this snippet in the build pipeline.
name, retries, timeoutMs, verbose
nightly-sync: retries=2 timeout=30000msWhat the code does
The top-level object literal pins name, retries, timeoutMs, verbose as plain values — no derivation, no imports behind them.
Statements that matter
type ConfigKeys = keyof PipelineConfig;
Interactive Tools for This Topic
Dotenv File ParserParse .env file content into a JSON object, handling comments, export prefixes, single quotes, and double-quoted escape sequences.INI File ParserParse INI configuration files into nested JSON with section objects, global keys, and both semicolon and hash comment support.JSON to TOML ConverterTurn a JSON object into TOML, emitting nested objects as tables and arrays of objects as array-of-tables blocks.JSON to YAML ConverterConvert JSON to readable YAML with nested block mappings, sequences, and quoting only where it is required.
FAQ
- Is there an install step between copy and run?
- No. Between copying and running there is nothing left to do.
Report a problem with this exampleOpens a pre-filled issue on GitHub.