diff --git a/index.d.ts b/index.d.ts index 9ea9ed954..0d06f8339 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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'; diff --git a/readme.md b/readme.md index d4cb19127..0227dd669 100644 --- a/readme.md +++ b/readme.md @@ -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. diff --git a/source/merge-deep.d.ts b/source/merge-deep.d.ts index 0c4ed64f8..44c1c5b84 100644 --- a/source/merge-deep.d.ts +++ b/source/merge-deep.d.ts @@ -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 = ConditionalSimplifyDeep, 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. diff --git a/source/omit-deep.d.ts b/source/omit-deep.d.ts index d1831e5aa..35258730a 100644 --- a/source/omit-deep.d.ts +++ b/source/omit-deep.d.ts @@ -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'; /** diff --git a/source/simplify-deep.d.ts b/source/simplify-deep.d.ts new file mode 100644 index 000000000..2479a89d0 --- /dev/null +++ b/source/simplify-deep.d.ts @@ -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; +// 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 = ConditionalSimplifyDeep, object>; diff --git a/source/simplify.d.ts b/source/simplify.d.ts index a456364d2..f31f6da6e 100644 --- a/source/simplify.d.ts +++ b/source/simplify.d.ts @@ -52,7 +52,7 @@ fn(someInterface as Simplify); // Good: transform an `interface` ``` @link https://github.com/microsoft/TypeScript/issues/15300 - +@see SimplifyDeep @category Object */ export type Simplify = {[KeyType in keyof T]: T[KeyType]} & {}; diff --git a/test-d/simplify-deep.ts b/test-d/simplify-deep.ts new file mode 100644 index 000000000..988746450 --- /dev/null +++ b/test-d/simplify-deep.ts @@ -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>(flattenProperties);