Skip to content

Commit

Permalink
Add OverrideProperties type (#597)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
nidomiro and sindresorhus committed Apr 23, 2023
1 parent 9feb8c8 commit c365837
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Expand Up @@ -72,6 +72,7 @@ export type {StringKeyOf} from './source/string-key-of';
export type {Exact} from './source/exact';
export type {ReadonlyTuple} from './source/readonly-tuple';
export type {OptionalKeysOf} from './source/optional-keys-of';
export type {OverrideProperties} from './source/override-properties';
export type {HasOptionalKeys} from './source/has-optional-keys';
export type {RequiredKeysOf} from './source/required-keys-of';
export type {HasRequiredKeys} from './source/has-required-keys';
Expand Down
1 change: 1 addition & 0 deletions readme.md
Expand Up @@ -135,6 +135,7 @@ Click the type names for complete docs.
- [`Merge`](source/merge.d.ts) - Merge two types into a new type. Keys of the second type overrides keys of the first type.
- [`MergeDeep`](source/merge-deep.d.ts) - Merge two objects or two arrays/tuples recursively into a new type.
- [`MergeExclusive`](source/merge-exclusive.d.ts) - Create a type that has mutually exclusive keys.
- [`OverrideProperties`](source/override-properties.d.ts) - Override only existing properties of the given type. Similar to `Merge`, but enforces that the original type has the properties you want to override.
- [`RequireAtLeastOne`](source/require-at-least-one.d.ts) - Create a type that requires at least one of the given keys.
- [`RequireExactlyOne`](source/require-exactly-one.d.ts) - Create a type that requires exactly a single key of the given keys and disallows more.
- [`RequireAllOrNone`](source/require-all-or-none.d.ts) - Create a type that requires all of the given keys or none of the given keys.
Expand Down
26 changes: 26 additions & 0 deletions source/override-properties.d.ts
@@ -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>;
14 changes: 14 additions & 0 deletions test-d/override-properties.ts
@@ -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}>;
});

0 comments on commit c365837

Please sign in to comment.