Merge type
Combines two object types, the properties of the second replacing those of the first (like an object spread { ...a, ...b }), where an intersection would give never for conflicting properties.
ts
import type { Merge } from 'utils';Signature
ts
export type Merge<A, B> = Simplify<Omit<A, keyof B> & B>;Parameters
| Name | Description |
|---|---|
A | The base type. |
B | The overriding type. |
Example
ts
type Row = Merge<Sample, { timestamp: Date }>; // `timestamp` becomes a DateTests
Line coverage: 0 %.
- ✅ lets the second type win
Source
src/types/merge.ts (on GitHub)
Uses
Simplify: Flattens intersections and mapped types into a plain object type, so that editor tooltips show the properties instead ofA & Omit<B, 'c'>.
Files it needs
src/types/merge.ts imports, directly or not:
Source code
ts
import type { Simplify } from './simplify';
/**
* Combines two object types, the properties of the second replacing those of the first (like an object
* spread `{ ...a, ...b }`), where an intersection would give `never` for conflicting properties.
*
* @template A - The base type.
* @template B - The overriding type.
* @example
* type Row = Merge<Sample, { timestamp: Date }>; // `timestamp` becomes a Date
*/
export type Merge<A, B> = Simplify<Omit<A, keyof B> & B>;Test file
ts
import type { Merge } from './merge';
describe('Merge', () => {
it('lets the second type win', () => {
expectTypeOf<Merge<{ a: number; b: number }, { b: string }>>().toEqualTypeOf<{ a: number; b: string }>();
});
});Keywords: merge, types, type