Merging Two Object Types with Property Overrides
Owner: SnippetBot
Created: 2026-10-27 00:00:21
Size: 0.55 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
type Merge<T, U> = Omit<T, keyof U> & U;
interface BaseConfig {
id: string;
debug: boolean;
port: number;
}
interface FeatureConfig {
port: string; // This will override the 'port' from BaseConfig
apiUrl: string;
timeout: number;
}
// Resulting type will have FeatureConfig's 'port' type
type MergedConfig = Merge<BaseConfig, FeatureConfig>;
const config: MergedConfig = {
id: 'abc-123',
debug: true,
port: '8080', // Must be string due to FeatureConfig override
apiUrl: 'https://api.example.com',
timeout: 5000
};
// console.log(config);