Skip to content

Commit

Permalink
Add SimplifyDeep type (#882)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
Emiyaaaaa and sindresorhus authored Jun 4, 2024
1 parent 5ad462a commit 3a04fcf
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 7 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export type {SetReturnType} from './source/set-return-type';
export type {SetParameterType} from './source/set-parameter-type';
export type {Asyncify} from './source/asyncify';
export type {Simplify} from './source/simplify';
export type {SimplifyDeep} from './source/simplify-deep';
export type {Jsonify} from './source/jsonify';
export type {Jsonifiable} from './source/jsonifiable';
export type {Schema} from './source/schema';
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ Click the type names for complete docs.
- [`SetReturnType`](source/set-return-type.d.ts) - Create a function type with a return type of your choice and the same parameters as the given function type.
- [`SetParameterType`](source/set-parameter-type.d.ts) - Create a function that replaces some parameters with the given parameters.
- [`Simplify`](source/simplify.d.ts) - Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
- [`SimplifyDeep`](source/simplify-deep.d.ts) - Deeply simplifies an object type.
- [`Get`](source/get.d.ts) - Get a deeply-nested property from an object using a key path, like [Lodash's `.get()`](https://lodash.com/docs/latest#get) function.
- [`StringKeyOf`](source/string-key-of.d.ts) - Get keys of the given type as strings.
- [`Schema`](source/schema.d.ts) - Create a deep version of another object type where property values are recursively replaced into a given value type.
Expand Down
6 changes: 1 addition & 5 deletions source/merge-deep.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ import type {
} from './internal';
import type {UnknownRecord} from './unknown-record';
import type {EnforceOptional} from './enforce-optional';

/**
Deeply simplifies an object excluding iterables and functions. Used internally to improve the UX and accept both interfaces and type aliases as inputs.
*/
export type SimplifyDeep<Type> = ConditionalSimplifyDeep<Type, Function | Iterable<unknown>, object>;
import type {SimplifyDeep} from './simplify-deep';

/**
Try to merge two record properties or return the source property value, preserving `undefined` properties values in both cases.
Expand Down
2 changes: 1 addition & 1 deletion source/omit-deep.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import type {ExactKey, IsArrayReadonly, NonRecursiveType, SetArrayAccess, ToStri
import type {IsEqual} from './is-equal';
import type {IsNever} from './is-never';
import type {LiteralUnion} from './literal-union';
import type {SimplifyDeep} from './merge-deep';
import type {Paths} from './paths';
import type {SharedUnionFieldsDeep} from './shared-union-fields-deep';
import type {SimplifyDeep} from './simplify-deep';
import type {UnknownArray} from './unknown-array';

/**
Expand Down
51 changes: 51 additions & 0 deletions source/simplify-deep.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type {ConditionalSimplifyDeep} from './conditional-simplify';

/**
Deeply simplifies an object type.
Useful to flatten the type output to improve type hints shown in editors.
@example
```
import type {SimplifyDeep} from 'type-fest';
type Properties1 = {
height: number;
position: {
top: number;
bottom: number;
};
};
type Properties2 = {
width: number;
position: {
left: number;
right: number;
};
};
type Properties = Properties1 & Properties2;
// In your editor, hovering over `Props` will show the following:
//
// type Properties = Properties1 & Properties2;
type SimplifyDeepProperties = SimplifyDeep<Properties1 & Properties2>;
// But if wrapped in SimplifyDeep, hovering over `Props` will show a flattened object with all the properties:
//
// SimplifyDeepProperties = {
// height: number;
// width: number;
// position: {
// top: number;
// bottom: number;
// left: number;
// right: number;
// };
// };
```
@see Simplify
@category Object
*/
export type SimplifyDeep<Type> = ConditionalSimplifyDeep<Type, Function | Iterable<unknown>, object>;
2 changes: 1 addition & 1 deletion source/simplify.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface`
```
@link https://github.com/microsoft/TypeScript/issues/15300
@see SimplifyDeep
@category Object
*/
export type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
31 changes: 31 additions & 0 deletions test-d/simplify-deep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {expectType} from 'tsd';
import type {SimplifyDeep} from '../index';

type Properties1 = {
height: number;
position: {
top: number;
bottom: number;
};
};

type Properties2 = {
width: number;
position: {
left: number;
right: number;
};
};

// Flatten the type output to improve type hints shown in editors.
declare const flattenProperties: {
height: number;
width: number;
position: {
top: number;
bottom: number;
left: number;
right: number;
};
};
expectType<SimplifyDeep<Properties1 & Properties2>>(flattenProperties);

0 comments on commit 3a04fcf

Please sign in to comment.