Skip to content

Commit a7df7d4

Browse files
committed
fix: string-slice with unions
1 parent 266413a commit a7df7d4

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

source/string-slice.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ StringSlice<'abcde', -2, -1>;
2828
*/
2929
export type StringSlice<
3030
S extends string,
31-
Start extends number = 0,
32-
End extends number = StringToArray<S>['length'],
31+
Start extends number = never,
32+
End extends number = never,
3333
> = string extends S
3434
? string
3535
: ArraySlice<StringToArray<S>, Start, End> extends infer R extends readonly string[]

test-d/string-slice.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,29 @@ expectType<StringSlice<'abcde', 100, 1>>('');
1515
expectType<StringSlice<string>>(null! as string);
1616
expectType<StringSlice<string, 1>>(null! as string);
1717
expectType<StringSlice<string, 1, 2>>(null! as string);
18+
19+
// Unions
20+
// String is union
21+
expectType<StringSlice<'abcd' | '0123456789'>>({} as 'abcd' | '0123456789');
22+
expectType<StringSlice<'abcd' | '0123456789', 0, 2>>({} as 'ab' | '01');
23+
24+
// Start is union
25+
expectType<StringSlice<'abcd', 1 | 3>>({} as 'bcd' | 'd');
26+
expectType<StringSlice<'abcd', 0 | 2, 3>>({} as 'abc' | 'c');
27+
28+
// End is union
29+
expectType<StringSlice<'abcd', 1, 2 | 3>>({} as 'b' | 'bc');
30+
31+
// String and start are unions
32+
expectType<StringSlice<'abcd' | '0123456789', 1 | 2>>({} as 'bcd' | 'cd' | '123456789' | '23456789');
33+
34+
// String and end are unions
35+
expectType<StringSlice<'abcd' | '0123456789', 1, 2 | 3>>({} as 'b' | 'bc' | '1' | '12');
36+
37+
// Start and end are unions
38+
expectType<StringSlice<'abcd', 0 | 1, 2 | 3>>({} as 'ab' | 'abc' | 'b' | 'bc');
39+
40+
// String, start and end are unions
41+
expectType<StringSlice<'abcd' | '0123456789', 0 | 1, 2 | 3>>(
42+
{} as 'ab' | 'abc' | 'b' | 'bc' | '01' | '012' | '1' | '12',
43+
);

0 commit comments

Comments
 (0)