UNPKG

1.23 kBTypeScriptView Raw
1import type {Merge} from './merge';
2
3/**
4Override existing properties of the given type. Similar to `Merge`, but enforces that the original type has the properties you want to override.
5
6This is useful when you want to override existing properties with a different type and make sure that these properties really exist in the original.
7
8@example
9```
10type Foo = {
11 a: string
12 b: string
13}
14type Bar = OverrideProperties<Foo, {b: number}>
15//=> {a: string, b: number}
16
17type Baz = OverrideProperties<Foo, {c: number}>
18// Error, type '{ c: number; }' does not satisfy the constraint '{ c: never; }'
19
20type Fizz = OverrideProperties<Foo, {b: number; c: number}>
21// Error, type '{ b: number; c: number; }' does not satisfy the constraint '{ b: number; c: never; }'
22```
23
24@category Object
25*/
26export type OverrideProperties<
27 TOriginal,
28 // This first bit where we use `Partial` is to enable autocomplete
29 // and the second bit with the mapped type is what enforces that we don't try
30 // to override properties that doesn't exist in the original type.
31 TOverride extends Partial<Record<keyof TOriginal, unknown>> & {
32 [Key in keyof TOverride]: Key extends keyof TOriginal
33 ? TOverride[Key]
34 : never;
35 },
36> = Merge<TOriginal, TOverride>;