Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make Join work with const tuple values #533

Merged
merged 1 commit into from Dec 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);