Skip to content

export ConditionalSimplify and ConditionalSimplifyDeep #1094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ export type {StringRepeat} from './source/string-repeat';
export type {Includes} from './source/includes';
export type {Get} from './source/get';
export type {LastArrayElement} from './source/last-array-element';
export type {ConditionalSimplify} from './source/conditional-simplify';
export type {ConditionalSimplifyDeep} from './source/conditional-simplify-deep';

// Miscellaneous
export type {GlobalThis} from './source/global-this';
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ Click the type names for complete docs.
- [`NonEmptyTuple`](source/non-empty-tuple.d.ts) - Matches any non-empty tuple.
- [`FindGlobalType`](source/find-global-type.d.ts) - Tries to find the type of a global with the given name.
- [`FindGlobalInstanceType`](source/find-global-type.d.ts) - Tries to find one or more types from their globally-defined constructors.
- [`ConditionalSimplify`](source/conditional-simplify.d.ts) - Simplifies a type while including and/or excluding certain types from being simplified.
- [`ConditionalSimplifyDeep`](source/conditional-simplify-deep.d.ts) - Recursively simplifies a type while including and/or excluding certain types from being simplified.

### Type Guard

Expand Down
2 changes: 1 addition & 1 deletion source/conditional-pick-deep.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {IsEqual} from './is-equal';
import type {ConditionalExcept} from './conditional-except';
import type {ConditionalSimplifyDeep} from './conditional-simplify';
import type {ConditionalSimplifyDeep} from './conditional-simplify-deep';
import type {UnknownRecord} from './unknown-record';
import type {EmptyObject} from './empty-object';
import type {ApplyDefaultOptions, IsPlainObject} from './internal';
Expand Down
63 changes: 63 additions & 0 deletions source/conditional-simplify-deep.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
Recursively simplifies a type while including and/or excluding certain types from being simplified.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please preserve the code example like it was originally, this includes empty lines.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@romanywu I was talking in general, not just this example. All the code.


@example
```ts
import type {ConditionalSimplifyDeep} from 'type-fest';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import type {ConditionalSimplifyDeep} from 'type-fest';
import type {ConditionalSimplifyDeep} from 'type-fest';

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Etc etc.

type TypeA = {
foo: {
a: string
}
};
type TypeB = {
foo: {
b: string
}
};
type SimplifyDeepTypeAB = ConditionalSimplifyDeep<TypeA & TypeB, never, object>;
//=> {foo: {a: string, b: string}}
```
@example
```ts
import type {ConditionalSimplifyDeep} from 'type-fest';
type SomeComplexType1 = {
a1: string
b1: number
c1: boolean
...
}
type SomeComplexType2 = {
a2: string
b2: number
c2: boolean
...
}
type TypeA = {
foo: {
a: string
complexType: SomeComplexType1
}
};
type TypeB = {
foo: {
b: string
complexType: SomeComplexType2
}
};
type SimplifyDeepTypeAB = ConditionalSimplifyDeep<TypeA & TypeB, SomeComplexType1 | SomeComplexType2, object>;
//=> {
// foo: {
// a: string,
// b: string,
// complexType: SomeComplexType1 | SomeComplexType2
// }
// }
```
@see SimplifyDeep
@category Object
*/
export type ConditionalSimplifyDeep<Type, ExcludeType = never, IncludeType = unknown> = Type extends ExcludeType
? Type
: Type extends IncludeType
? {[TypeKey in keyof Type]: ConditionalSimplifyDeep<Type[TypeKey], ExcludeType, IncludeType>}
: Type;
52 changes: 28 additions & 24 deletions source/conditional-simplify.d.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
/**
Simplifies a type while including and/or excluding certain types from being simplified. Useful to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.

This type is **experimental** and was introduced as a result of this {@link https://github.com/sindresorhus/type-fest/issues/436 issue}. It should be used with caution.

@internal
@experimental
@see Simplify
Simplifies a type while including and/or excluding certain types from being simplified.
Useful to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
@example
```ts
import type {ConditionalSimplify} from 'type-fest';
type TypeA = {
a: string
}
type TypeB = {
b: string
}
type TypeAB = TypeA & TypeB
//=> {a: string} & {b: string}
type SimplifyTypeAB = ConditionalSimplify<TypeAB, never, object>
//=> {a: string, b: string}
```
@example
```ts
import type {ConditionalSimplify} from 'type-fest';
type Simplify<T> = ConditionalSimplify<T, Set<unknown> | Map<unknown, unknown> | unknown[], object>;
type A = Simplify<Set<number> & Set<string>>;
//=> Set<number> & Set<string>
type B = Simplify<Map<number, number> & Map<string, string>>;
//=> Map<number, number> & Map<string, string>
type C = Simplify<{ a: number } & { b: string }>;
//=> {a: number, b: string}
```
@see ConditionalSimplifyDeep
@category Object
*/
export type ConditionalSimplify<Type, ExcludeType = never, IncludeType = unknown> = Type extends ExcludeType
? Type
: Type extends IncludeType
? {[TypeKey in keyof Type]: Type[TypeKey]}
: Type;

/**
Recursively simplifies a type while including and/or excluding certain types from being simplified.

This type is **experimental** and was introduced as a result of this {@link https://github.com/sindresorhus/type-fest/issues/436 issue}. It should be used with caution.

See {@link ConditionalSimplify} for usages and examples.

@internal
@experimental
@category Object
*/
export type ConditionalSimplifyDeep<Type, ExcludeType = never, IncludeType = unknown> = Type extends ExcludeType
? Type
: Type extends IncludeType
? {[TypeKey in keyof Type]: ConditionalSimplifyDeep<Type[TypeKey], ExcludeType, IncludeType>}
: Type;
2 changes: 1 addition & 1 deletion source/merge-deep.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {ConditionalSimplifyDeep} from './conditional-simplify';
import type {ConditionalSimplifyDeep} from './conditional-simplify-deep';
import type {OmitIndexSignature} from './omit-index-signature';
import type {PickIndexSignature} from './pick-index-signature';
import type {Merge} from './merge';
Expand Down
2 changes: 1 addition & 1 deletion source/simplify-deep.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {ConditionalSimplifyDeep} from './conditional-simplify';
import type {ConditionalSimplifyDeep} from './conditional-simplify-deep';
import type {NonRecursiveType} from './internal';

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

type Position = {top: number; left: number};
type Size = {width: number; height: number};

// In your editor, hovering over `PositionAndSizeSimplified` will show a simplified object with all the properties.
type PositionAndSizeIntersection = Position & Size;

const position = {top: 120, left: 240};
const size = {width: 480, height: 600};
const positionAndSize = {...position, ...size};

// Should simplify interface deeply.
type SomeNode = {
parent: PositionAndSizeIntersection;
childs: Array<{parent: PositionAndSizeIntersection}>;
Copy link
Preview

Copilot AI Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming 'childs' to 'children' for correct plural form.

Copilot uses AI. Check for mistakes.

};

// In your editor, hovering over `SomeNodeSimplified` will show a simplified object with all the properties.
type SomeNodeSimplified = ConditionalSimplifyDeep<SomeNode>;

const someNode = {parent: positionAndSize, childs: [{parent: positionAndSize}, {parent: positionAndSize}]};
expectType<SomeNodeSimplified>(someNode);

// Should simplify interface deeply excluding Function type.
// TODO: Convert this to a `type`.
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface MovablePosition extends Position {
move(position: Position): Position;
}

type MovableCollection = {
position: MovablePosition;
top: {position: MovablePosition; size: Size};
left: {position: MovablePosition; size: Size};
};

type MovableNodeSimplifiedFail = ConditionalSimplifyDeep<MovableCollection>;
type MovableNodeSimplifiedPass = ConditionalSimplifyDeep<MovableCollection, Function>;

declare const movableNodeSimplifiedFail: MovableNodeSimplifiedFail;
declare const movableNodeSimplifiedPass: MovableNodeSimplifiedPass;

expectNotAssignable<MovableCollection>(movableNodeSimplifiedFail);
expectType<MovableCollection>(movableNodeSimplifiedPass);

const movablePosition = {
top: 42,
left: 42,
move(position: Position) {
return position;
},
};

const movableNode = {
position: movablePosition,
top: {position: movablePosition, size},
left: {position: movablePosition, size},
};

expectType<MovableNodeSimplifiedPass>(movableNode);

// Should exclude `Function` and `Size` type (mainly visual, mouse over the statement).
type ExcludeFunctionAndSize1 = ConditionalSimplifyDeep<MovableCollection, Function | Size>;
expectType<ExcludeFunctionAndSize1>(movableNode);

// Same as above but using `IncludeType` parameter (mainly visual, mouse over the statement).
type ExcludeFunctionAndSize2 = ConditionalSimplifyDeep<MovableCollection, Function, MovableCollection | Position>;
expectType<ExcludeFunctionAndSize2>(movableNode);
65 changes: 1 addition & 64 deletions test-d/conditional-simplify.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,20 @@
import {expectNotAssignable, expectType} from 'tsd';
import type {ConditionalSimplify, ConditionalSimplifyDeep} from '../source/conditional-simplify';
import type {ConditionalSimplify} from '../index';

type Position = {top: number; left: number};
type Size = {width: number; height: number};

// In your editor, hovering over `PositionAndSizeSimplified` will show a simplified object with all the properties.
type PositionAndSizeIntersection = Position & Size;
type PositionAndSizeSimplified = ConditionalSimplify<PositionAndSizeIntersection>;

const position = {top: 120, left: 240};
const size = {width: 480, height: 600};
const positionAndSize = {...position, ...size};
expectType<PositionAndSizeSimplified>(positionAndSize);

// Exclude function type to be simplified.
type SomeFunction = (type: string) => string;
type SimplifiedFunctionFail = ConditionalSimplify<SomeFunction>; // Return '{}'
type SimplifiedFunctionPass = ConditionalSimplify<SomeFunction, Function>; // Return '(type: string) => string'

declare const simplifiedFunctionFail: SimplifiedFunctionFail;
declare const simplifiedFunctionPass: SimplifiedFunctionPass;

expectNotAssignable<SomeFunction>(simplifiedFunctionFail);
expectType<SomeFunction>(simplifiedFunctionPass);

// Should simplify interface deeply.
type SomeNode = {
parent: PositionAndSizeIntersection;
childs: Array<{parent: PositionAndSizeIntersection}>;
};

// In your editor, hovering over `SomeNodeSimplified` will show a simplified object with all the properties.
type SomeNodeSimplified = ConditionalSimplifyDeep<SomeNode>;

const someNode = {parent: positionAndSize, childs: [{parent: positionAndSize}, {parent: positionAndSize}]};
expectType<SomeNodeSimplified>(someNode);

// Should simplify interface deeply excluding Function type.
// TODO: Convert this to a `type`.
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface MovablePosition extends Position {
move(position: Position): Position;
}

type MovableCollection = {
position: MovablePosition;
top: {position: MovablePosition; size: Size};
left: {position: MovablePosition; size: Size};
};

type MovableNodeSimplifiedFail = ConditionalSimplifyDeep<MovableCollection>;
type MovableNodeSimplifiedPass = ConditionalSimplifyDeep<MovableCollection, Function>;

declare const movableNodeSimplifiedFail: MovableNodeSimplifiedFail;
declare const movableNodeSimplifiedPass: MovableNodeSimplifiedPass;

expectNotAssignable<MovableCollection>(movableNodeSimplifiedFail);
expectType<MovableCollection>(movableNodeSimplifiedPass);

const movablePosition = {
top: 42,
left: 42,
move(position: Position) {
return position;
},
};

const movableNode = {
position: movablePosition,
top: {position: movablePosition, size},
left: {position: movablePosition, size},
};

expectType<MovableNodeSimplifiedPass>(movableNode);

// Should exclude `Function` and `Size` type (mainly visual, mouse over the statement).
type ExcludeFunctionAndSize1 = ConditionalSimplifyDeep<MovableCollection, Function | Size>;
expectType<ExcludeFunctionAndSize1>(movableNode);

// Same as above but using `IncludeType` parameter (mainly visual, mouse over the statement).
type ExcludeFunctionAndSize2 = ConditionalSimplifyDeep<MovableCollection, Function, MovableCollection | Position>;
expectType<ExcludeFunctionAndSize2>(movableNode);