Skip to content

Commit

Permalink
Join: Fix usage of single element const tuple (#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmmmwh committed Feb 26, 2023
1 parent f81eec6 commit 747e29b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
6 changes: 3 additions & 3 deletions source/join.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ const path: Join<[1, 2, 3], '.'> = [1, 2, 3].join('.');
@category Template literal
*/
export type Join<
Strings extends Readonly<Array<string | number>>,
Strings extends ReadonlyArray<string | number>,
Delimiter extends string,
> = Strings extends []
? ''
: Strings extends [string | number]
: Strings extends readonly [string | number]
? `${Strings[0]}`
: Strings extends readonly [
string | number,
...infer Rest extends Array<string | number>,
...infer Rest extends ReadonlyArray<string | number>,
]
? `${Strings[0]}${Delimiter}${Join<Rest, Delimiter>}`
: string;
16 changes: 14 additions & 2 deletions test-d/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,24 @@ const emptyInput: Join<[], '.'> = '';
expectType<''>(emptyInput);
expectNotAssignable<'foo'>(emptyInput);

// Typeof of const tuple
// Single input with string[].
const singleStringArray = ['test'];
const singleInput: Join<typeof singleStringArray, '.'> = 'test';
expectType<string>(singleInput);
expectNotAssignable<'test'>(singleInput);

// Single input with const tuple.
const singleTuple = ['test'] as const;
const singleTupleJoined: Join<typeof singleTuple, '.'> = 'test';
expectType<'test'>(singleTupleJoined);
expectNotAssignable<'test.'>(singleTupleJoined);

// Typeof of const tuple.
const tuple = ['foo', 'bar', 'baz'] as const;
const joinedTuple: Join<typeof tuple, ','> = 'foo,bar,baz';
expectType<'foo,bar,baz'>(joinedTuple);

// Typeof of string[]
// Typeof of string[].
const stringArray = ['foo', 'bar', 'baz'];
const joinedStringArray: Join<typeof stringArray, ','> = '';
expectType<string>(joinedStringArray);
Expand Down

0 comments on commit 747e29b

Please sign in to comment.