Skip to content

Commit

Permalink
ArraySlice: Fix support for union input (#986)
Browse files Browse the repository at this point in the history
  • Loading branch information
DIDA-lJ authored Nov 23, 2024
1 parent c85bc12 commit 0efbae3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 7 additions & 5 deletions source/array-slice.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ export type ArraySlice<
Array_ extends readonly unknown[],
Start extends number = never,
End extends number = never,
> = And<IsEqual<Start, never>, IsEqual<End, never>> extends true
? Array_
: number extends Array_['length']
? VariableLengthArraySliceHelper<Array_, Start, End>
: ArraySliceHelper<Array_, IsEqual<Start, never> extends true ? 0 : Start, IsEqual<End, never> extends true ? Array_['length'] : End>;
> = Array_ extends unknown // To distributive type
? And<IsEqual<Start, never>, IsEqual<End, never>> extends true
? Array_
: number extends Array_['length']
? VariableLengthArraySliceHelper<Array_, Start, End>
: ArraySliceHelper<Array_, IsEqual<Start, never> extends true ? 0 : Start, IsEqual<End, never> extends true ? Array_['length'] : End>
: never; // Never happens

type VariableLengthArraySliceHelper<
Array_ extends readonly unknown[],
Expand Down
2 changes: 2 additions & 0 deletions test-d/array-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {expectType} from 'tsd';
import type {ArraySlice} from '../index';

expectType<ArraySlice<[0, 1, 2, 3]>>([0, 1, 2, 3]);
expectType<ArraySlice<[0, 1, 2] | [0, 1, 2, 3], 0>>({} as [0, 1, 2] | [0, 1, 2, 3]);
expectType<ArraySlice<[0, 1, 2] | [3, 2, 1, 0], 0, 2>>({} as [0, 1] | [3, 2]);
expectType<ArraySlice<[0, 1, 2, 3]>>([0, 1, 2, 3]);
expectType<ArraySlice<[0, 1, 2, 3], 1>>([1, 2, 3]);
expectType<ArraySlice<[0, 1, 2, 3], 1, 2>>([1]);
Expand Down

0 comments on commit 0efbae3

Please sign in to comment.