Skip to content

Commit

Permalink
MergeDeep: Fix compatibility with TypeScript 5.4 (#807)
Browse files Browse the repository at this point in the history
  • Loading branch information
skarab42 committed Jan 30, 2024
1 parent eccf171 commit 5f6165a
Showing 1 changed file with 14 additions and 2 deletions.
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

0 comments on commit 5f6165a

Please sign in to comment.