Simplify Flattens Intersection Types
A one-line mapped type flattens Defaults & Overrides intersections so hover tooltips and ExpectEqual comparisons show a single clean object shape.
Code
// A homomorphic mapped type copies every key once, collapsing an
// intersection of object types into one flat shape for hover and compare.
type Simplify<T> = { [K in keyof T]: T[K] } & {};
interface BaseConfig {
retries: number;
timeoutMs: number;
}
type Overrides = { timeoutMs?: number; label?: string };
// Raw intersections keep both members listed; Simplify merges them
type ResolvedRaw = BaseConfig & Required<Overrides>;
type Resolved = Simplify<ResolvedRaw>;
type ExpectEqual<T, U> =
(<V>() => V extends T ? 1 : 2) extends <V>() => V extends U ? 1 : 2 ? true : never;
type Checks = [
ExpectEqual<Resolved, { retries: number; timeoutMs: number; label: string }>,
];
type _Checks = Checks;
const resolved: Resolved = { retries: 3, timeoutMs: 1_500, label: 'billing' };
console.log(resolved.retries, resolved.timeoutMs, resolved.label);
console.log('keys:', Object.keys(resolved).sort().join(','));Output
Captured from a sandboxed run of this snippet in the build pipeline.
3 1500 billing
keys: label,retries,timeoutMsRuntime behavior
The top-level object literal pins retries, label as plain values — no derivation, no imports behind them.
Watch out for
- No dependencies section needed; the standard library is the whole story.
Applying it
Output lines are prefixed (keys:), so each printed result carries the name of its step.
Statements that matter
(<V>() => V extends T ? 1 : 2) extends <V>() => V extends U ? 1 : 2 ? true : never;
type Simplify<T> = { [K in keyof T]: T[K] } & {};
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 anything beyond the standard library required?
- Nothing beyond the standard library — the snippet is dependency-free.
Report a problem with this exampleOpens a pre-filled issue on GitHub.