Skip to content
Closed
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
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ Click the type names for complete docs.
- [`Entries`](source/entries.d.ts) - Create a type that represents the type of the entries of a collection.
- [`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.
- [`Simplify`](source/simplify.d.ts) - Flattens a complex object type to make it more readable and improve editor IntelliSense. And also to transform an interface into a type to aid with assignability.
- [`SimplifyDeep`](source/simplify-deep.d.ts) - Deeply simplifies an object type.
- [`SimplifyUnion`](source/simplify-union.d.ts) - Flattens a complex union type to make it more readable and improve editor IntelliSense.
- [`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.
- [`KeyAsString`](source/key-as-string.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
4 changes: 2 additions & 2 deletions source/all-union-fields.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {NonRecursiveType, ReadonlyKeysOfUnion, ValueOfUnion} from './internal/index.d.ts';
import type {KeysOfUnion} from './keys-of-union.d.ts';
import type {SharedUnionFields} from './shared-union-fields.d.ts';
import type {Simplify} from './simplify.d.ts';
import type {_Simplify} from './simplify.d.ts';
import type {UnknownArray} from './unknown-array.d.ts';

/**
Expand Down Expand Up @@ -72,7 +72,7 @@ Extract<Union, NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<un
? Exclude<Union, SkippedMembers> extends infer RelevantMembers
?
| SkippedMembers
| Simplify<
| _Simplify<
// Include fields that are common in all union members
SharedUnionFields<RelevantMembers> &
// Include readonly fields present in any union member
Expand Down
4 changes: 2 additions & 2 deletions source/internal/enforce-optional.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {Simplify} from '../simplify.d.ts';
import type {_Simplify} from '../simplify.d.ts';

// Returns `never` if the key is optional otherwise return the key type.
type RequiredFilter<Type, Key extends keyof Type> = undefined extends Type[Key]
Expand Down Expand Up @@ -40,7 +40,7 @@ type FooBar = EnforceOptional<Foo>;
@internal
@category Object
*/
export type EnforceOptional<ObjectType> = Simplify<{
export type EnforceOptional<ObjectType> = _Simplify<{
[Key in keyof ObjectType as RequiredFilter<ObjectType, Key>]: ObjectType[Key]
} & {
[Key in keyof ObjectType as OptionalFilter<ObjectType, Key>]?: Exclude<ObjectType[Key], undefined>
Expand Down
8 changes: 4 additions & 4 deletions source/internal/object.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {Simplify} from '../simplify.d.ts';
import type {_Simplify} from '../simplify.d.ts';
import type {UnknownArray} from '../unknown-array.d.ts';
import type {IsEqual} from '../is-equal.d.ts';
import type {KeysOfUnion} from '../keys-of-union.d.ts';
Expand Down Expand Up @@ -79,7 +79,7 @@ type OptionalizedUser = UndefinedToOptional<User>;
// }
```
*/
export type UndefinedToOptional<T extends object> = Simplify<
export type UndefinedToOptional<T extends object> = _Simplify<
{
// Property is not a union with `undefined`, keep it as-is.
[Key in keyof Pick<T, FilterDefinedKeys<T>>]: T[Key];
Expand Down Expand Up @@ -220,12 +220,12 @@ type Result = ApplyDefaultOptions<PathsOptions, DefaultPathsOptions, SpecifiedOp
*/
export type ApplyDefaultOptions<
Options extends object,
Defaults extends Simplify<Omit<Required<Options>, RequiredKeysOf<Options>> & Partial<Record<RequiredKeysOf<Options>, never>>>,
Defaults extends _Simplify<Omit<Required<Options>, RequiredKeysOf<Options>> & Partial<Record<RequiredKeysOf<Options>, never>>>,
SpecifiedOptions extends Options,
> =
If<IsAny<SpecifiedOptions>, Defaults,
If<IsNever<SpecifiedOptions>, Defaults,
Simplify<Merge<Defaults, {
_Simplify<Merge<Defaults, {
[Key in keyof SpecifiedOptions
as Key extends OptionalKeysOf<Options> ? undefined extends SpecifiedOptions[Key] ? never : Key : Key
]: SpecifiedOptions[Key]
Expand Down
4 changes: 2 additions & 2 deletions source/merge.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {OmitIndexSignature} from './omit-index-signature.d.ts';
import type {PickIndexSignature} from './pick-index-signature.d.ts';
import type {Simplify} from './simplify.d.ts';
import type {_Simplify} from './simplify.d.ts';

// Merges two objects without worrying about index signatures.
type SimpleMerge<Destination, Source> = {
Expand Down Expand Up @@ -42,7 +42,7 @@ export type FooBar = Merge<Foo, Bar>;
@category Object
*/
export type Merge<Destination, Source> =
Simplify<
_Simplify<
SimpleMerge<PickIndexSignature<Destination>, PickIndexSignature<Source>>
& SimpleMerge<OmitIndexSignature<Destination>, OmitIndexSignature<Source>>
>;
Expand Down
6 changes: 3 additions & 3 deletions source/pick-deep.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {TupleOf} from './tuple-of.d.ts';
import type {BuildObject, NonRecursiveType, ObjectValue} from './internal/index.d.ts';
import type {IsNever} from './is-never.d.ts';
import type {Paths} from './paths.d.ts';
import type {Simplify} from './simplify.d.ts';
import type {_Simplify} from './simplify.d.ts';
import type {UnionToIntersection} from './union-to-intersection.d.ts';
import type {UnknownArray} from './unknown-array.d.ts';

Expand Down Expand Up @@ -86,7 +86,7 @@ export type PickDeep<T, PathUnion extends Paths<T>> =
}[PathUnion]
>
: T extends object
? Simplify<UnionToIntersection<{
? _Simplify<UnionToIntersection<{
[P in PathUnion]: InternalPickDeep<T, P>;
}[PathUnion]>>
: never;
Expand All @@ -98,7 +98,7 @@ type InternalPickDeep<T, Path extends string | number> =
T extends NonRecursiveType
? never
: T extends UnknownArray ? PickDeepArray<T, Path>
: T extends object ? Simplify<PickDeepObject<T, Path>>
: T extends object ? _Simplify<PickDeepObject<T, Path>>
: never;

/**
Expand Down
4 changes: 2 additions & 2 deletions source/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {ApplyDefaultOptions} from './internal/object.d.ts';
import type {IfNotAnyOrNever, NonRecursiveType} from './internal/type.d.ts';
import type {OptionalKeysOf} from './optional-keys-of.d.ts';
import type {Simplify} from './simplify.d.ts';
import type {_Simplify} from './simplify.d.ts';
import type {UnknownArray} from './unknown-array.d.ts';

/**
Expand Down Expand Up @@ -106,7 +106,7 @@ Internal helper for {@link _Schema}.

Recursively transforms the value of each property in objects and arrays.
*/
type SchemaHelper<Type, Value, Options extends Required<SchemaOptions>> = Simplify<{
type SchemaHelper<Type, Value, Options extends Required<SchemaOptions>> = _Simplify<{
[Key in keyof Type]: _Schema<
Key extends OptionalKeysOf<Type & object> ? Exclude<Type[Key], undefined> : Type[Key], // Remove `| undefined` when accessing optional properties
Value,
Expand Down
4 changes: 2 additions & 2 deletions source/set-field-type.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {ApplyDefaultOptions} from './internal/index.d.ts';
import type {Simplify} from './simplify.d.ts';
import type {_Simplify} from './simplify.d.ts';

export type SetFieldTypeOptions = {
/**
Expand Down Expand Up @@ -57,7 +57,7 @@ export type SetFieldType<BaseType, Keys extends keyof BaseType, NewType, Options
_SetFieldType<BaseType, Keys, NewType, ApplyDefaultOptions<SetFieldTypeOptions, DefaultSetFieldTypeOptions, Options>>;

type _SetFieldType<BaseType, Keys extends keyof BaseType, NewType, Options extends Required<SetFieldTypeOptions>> =
Simplify<{
_Simplify<{
[P in keyof BaseType]: P extends Keys ? NewType : BaseType[P];
} & (
// `Record` is used to remove property modifiers
Expand Down
4 changes: 2 additions & 2 deletions source/set-non-nullable-deep.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {NonRecursiveType, StringToNumber} from './internal/index.d.ts';
import type {Paths} from './paths.d.ts';
import type {SetNonNullable} from './set-non-nullable.d.ts';
import type {Simplify} from './simplify.d.ts';
import type {_Simplify} from './simplify.d.ts';
import type {UnionToTuple} from './union-to-tuple.d.ts';
import type {UnknownArray} from './unknown-array.d.ts';

Expand Down Expand Up @@ -80,6 +80,6 @@ type SetNonNullableDeepSinglePath<BaseType, KeyPath> =
? SetNonNullableDeepSinglePath<BaseType[Key], RestPath>
: BaseType[Key];
}
: Simplify<SetNonNullable<BaseType, (KeyPath | StringToNumber<KeyPath & string>) & keyof BaseType>>;
: _Simplify<SetNonNullable<BaseType, (KeyPath | StringToNumber<KeyPath & string>) & keyof BaseType>>;

export {};
4 changes: 2 additions & 2 deletions source/set-optional.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {Except} from './except.d.ts';
import type {HomomorphicPick} from './internal/index.d.ts';
import type {Simplify} from './simplify.d.ts';
import type {_Simplify} from './simplify.d.ts';

/**
Create a type that makes the given keys optional. The remaining keys are kept as is. The sister of the `SetRequired` type.
Expand Down Expand Up @@ -35,7 +35,7 @@ export type SetOptional<BaseType, Keys extends keyof BaseType> =

type _SetOptional<BaseType, Keys extends keyof BaseType> =
BaseType extends unknown // To distribute `BaseType` when it's a union type.
? Simplify<
? _Simplify<
// Pick just the keys that are readonly from the base type.
Except<BaseType, Keys> &
// Pick the keys that should be mutable from the base type and make them mutable.
Expand Down
4 changes: 2 additions & 2 deletions source/set-readonly.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {Except} from './except.d.ts';
import type {HomomorphicPick} from './internal/index.d.ts';
import type {Simplify} from './simplify.d.ts';
import type {_Simplify} from './simplify.d.ts';

/**
Create a type that makes the given keys readonly. The remaining keys are kept as is.
Expand Down Expand Up @@ -35,7 +35,7 @@ export type SetReadonly<BaseType, Keys extends keyof BaseType> =

export type _SetReadonly<BaseType, Keys extends keyof BaseType> =
BaseType extends unknown // To distribute `BaseType` when it's a union type.
? Simplify<
? _Simplify<
Except<BaseType, Keys> &
Readonly<HomomorphicPick<BaseType, Keys>>
>
Expand Down
4 changes: 2 additions & 2 deletions source/set-required.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {Except} from './except.d.ts';
import type {If} from './if.d.ts';
import type {HomomorphicPick, IsArrayReadonly} from './internal/index.d.ts';
import type {OptionalKeysOf} from './optional-keys-of.d.ts';
import type {Simplify} from './simplify.d.ts';
import type {_Simplify} from './simplify.d.ts';
import type {UnknownArray} from './unknown-array.d.ts';

/**
Expand Down Expand Up @@ -45,7 +45,7 @@ type _SetRequired<BaseType, Keys extends keyof BaseType> =
? SetArrayRequired<BaseType, Keys> extends infer ResultantArray
? If<IsArrayReadonly<BaseType>, Readonly<ResultantArray>, ResultantArray>
: never
: Simplify<
: _Simplify<
// Pick just the keys that are optional from the base type.
Except<BaseType, Keys> &
// Pick the keys that should be required from the base type and make them required.
Expand Down
4 changes: 2 additions & 2 deletions source/shared-union-fields.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {NonRecursiveType} from './internal/index.d.ts';
import type {IsNever} from './is-never.d.ts';
import type {IsUnion} from './is-union.d.ts';
import type {Simplify} from './simplify.d.ts';
import type {_Simplify} from './simplify.d.ts';
import type {UnknownArray} from './unknown-array.d.ts';

/**
Expand Down Expand Up @@ -72,7 +72,7 @@ Extract<Union, NonRecursiveType | ReadonlyMap<unknown, unknown> | ReadonlySet<un
| SkippedMembers
| (IsNever<RelevantMembers> extends true
? never
: Simplify<Pick<RelevantMembers, keyof RelevantMembers>>)
: _Simplify<Pick<RelevantMembers, keyof RelevantMembers>>)
: never
: never;

Expand Down
20 changes: 17 additions & 3 deletions source/simplify.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type {ConditionalSimplify} from './conditional-simplify.d.ts';
import type {NonRecursiveType} from './internal/type.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.
Flattens a complex object type to make it more readable and improve editor IntelliSense. And also to transform an interface into a type to aid with assignability.

@example
```
Expand Down Expand Up @@ -52,9 +55,20 @@ fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface`
```

@link https://github.com/microsoft/TypeScript/issues/15300
@see SimplifyDeep
@see {@link SimplifyDeep}
@category Object
*/
export type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
export type Simplify<Type> = ConditionalSimplify<
Type,
NonRecursiveType | Map<unknown, unknown> | Set<unknown>,
object
>;

/**
Internal variant of {@link Simplify `Simplify`}.

Use this version when defining or composing types internally; use the external one only when necessary.
*/
export type _Simplify<T> = {[K in keyof T]: T[K]} & {};

export {};
6 changes: 3 additions & 3 deletions source/spread.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {RequiredKeysOf} from './required-keys-of.d.ts';
import type {Simplify} from './simplify.d.ts';
import type {_Simplify} from './simplify.d.ts';

type SpreadObject<FirstType extends object, SecondType extends object> = {
[Key in keyof FirstType]: Key extends keyof SecondType
Expand Down Expand Up @@ -80,7 +80,7 @@ export type Spread<
> = FirstType extends TupleOrArray
? SecondType extends TupleOrArray
? SpreadTupleOrArray<FirstType, SecondType>
: Simplify<SpreadObject<FirstType, SecondType>>
: Simplify<SpreadObject<FirstType, SecondType>>;
: _Simplify<SpreadObject<FirstType, SecondType>>
: _Simplify<SpreadObject<FirstType, SecondType>>;

export {};
4 changes: 2 additions & 2 deletions source/writable.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {Except} from './except.d.ts';
import type {Simplify} from './simplify.d.ts';
import type {_Simplify} from './simplify.d.ts';

/**
Create a writable version of the given array type.
Expand Down Expand Up @@ -60,7 +60,7 @@ BaseType extends ReadonlyMap<infer KeyType, infer ValueType>
// Handle array
? WritableArray<BaseType>
// Handle object
: Simplify<
: _Simplify<
// Pick just the keys that are not writable from the base type.
Except<BaseType, Keys> &
// Pick the keys that should be writable from the base type and make them writable by removing the `readonly` modifier from the key.
Expand Down
Loading