Skip to content
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

fix: MergeDeep for typescript 5.4 #807

Merged
merged 5 commits into from
Jan 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions source/merge-deep.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ type MergeDeepRecord<
> = DoMergeDeepRecord<OmitIndexSignature<Destination>, OmitIndexSignature<Source>, Options>
& Merge<PickIndexSignature<Destination>, PickIndexSignature<Source>>;

// Helper to avoid computing ArrayTail twice.
type PickRestTypeHelper<Tail extends UnknownArrayOrTuple, Type> = Tail extends [] ? Type : PickRestType<Tail>;

/**
Pick the rest type.

Expand All @@ -76,9 +79,18 @@ type Rest5 = PickRestType<string[]>; // => string[]
```
*/
type PickRestType<Type extends UnknownArrayOrTuple> = number extends Type['length']
? ArrayTail<Type> extends [] ? Type : PickRestType<ArrayTail<Type>>
? PickRestTypeHelper<ArrayTail<Type>, Type>
: [];

// Helper to avoid computing ArrayTail twice.
type OmitRestTypeHelper<
Tail extends UnknownArrayOrTuple,
Type extends UnknownArrayOrTuple,
Result extends UnknownArrayOrTuple = [],
> = Tail extends []
? Result
: OmitRestType<Tail, [...Result, FirstArrayElement<Type>]>;

/**
Omit the rest type.

Expand All @@ -93,7 +105,7 @@ type Tuple6 = OmitRestType<string[]>; // => []
```
*/
type OmitRestType<Type extends UnknownArrayOrTuple, Result extends UnknownArrayOrTuple = []> = number extends Type['length']
? ArrayTail<Type> extends [] ? Result : OmitRestType<ArrayTail<Type>, [...Result, FirstArrayElement<Type>]>
? OmitRestTypeHelper<ArrayTail<Type>, Type, Result>
: Type;

// Utility to avoid picking two times the type.
Expand Down