Skip to content

Commit

Permalink
Join: Support const tuple values (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwelle committed Dec 25, 2022
1 parent 00b13f8 commit db81191
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions source/join.d.ts
Expand Up @@ -21,13 +21,13 @@ const path: Join<[1, 2, 3], '.'> = [1, 2, 3].join('.');
@category Template literal
*/
export type Join<
Strings extends Array<string | number>,
Strings extends Readonly<Array<string | number>>,
Delimiter extends string,
> = Strings extends []
? ''
: Strings extends [string | number]
? `${Strings[0]}`
: Strings extends [
: Strings extends readonly [
string | number,
...infer Rest extends Array<string | number>,
]
Expand Down
11 changes: 11 additions & 0 deletions test-d/join.ts
Expand Up @@ -21,3 +21,14 @@ expectNotAssignable<'foo.bar.baz'>(emptyDelimiter);
const emptyInput: Join<[], '.'> = '';
expectType<''>(emptyInput);
expectNotAssignable<'foo'>(emptyInput);

// 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[]
const stringArray = ['foo', 'bar', 'baz'];
const joinedStringArray: Join<typeof stringArray, ','> = '';
expectType<string>(joinedStringArray);
expectNotAssignable<'foo,bar,baz'>(joinedStringArray);

0 comments on commit db81191

Please sign in to comment.