Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
- Loading branch information
1 parent
9feb8c8
commit c365837
Showing
4 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type {Merge} from './merge'; | ||
|
||
/** | ||
Override existing properties of the given type. Similar to `Merge`, but enforces that the original type has the properties you want to override. | ||
This is useful when you want to override existing properties with a different type and make sure that these properties really exist in the original. | ||
@example | ||
``` | ||
type Foo = { | ||
a: string | ||
b: string | ||
} | ||
type Bar = OverrideProperties<Foo, {b: number}> | ||
//=> {a: string, b: number} | ||
type Baz = OverrideProperties<Foo, {c: number}> | ||
// error TS2559: Type '{c: number}' has no properties in common with type 'Partial{a: unknown; b: unknown}>'. | ||
``` | ||
@category Object | ||
*/ | ||
export type OverrideProperties< | ||
TOriginal, | ||
TOverride extends Partial<{[key in keyof TOriginal]: unknown}>, | ||
> = Merge<TOriginal, TOverride>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {expectError, expectType} from 'tsd'; | ||
import type {OverrideProperties} from '../source/override-properties'; | ||
|
||
type Foo = { | ||
a: number; | ||
b: string; | ||
}; | ||
|
||
const fixture: OverrideProperties<Foo, {b: number}> = {a: 1, b: 2}; | ||
expectType<{a: number; b: number}>(fixture); | ||
|
||
expectError(() => { | ||
type Bar = OverrideProperties<Foo, {c: number}>; | ||
}); |