Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28199b0
commit a001611
Showing
3 changed files
with
22 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,29 @@ | ||
/** | ||
Join an array of strings using the given string as delimiter. | ||
Join an array of strings and/or numbers using the given string as a delimiter. | ||
Use-case: Defining key paths in a nested object. For example, for dot-notation fields in MongoDB queries. | ||
@example | ||
``` | ||
import {Join} from 'type-fest'; | ||
// Mixed (strings & numbers) items; result is: 'foo.0.baz' | ||
const path: Join<['foo', 0, 'baz'], '.'> = ['foo', 0, 'baz'].join('.'); | ||
// Only string items; result is: 'foo.bar.baz' | ||
const path: Join<['foo', 'bar', 'baz'], '.'> = ['foo', 'bar', 'baz'].join('.'); | ||
// Only number items; result is: '1.2.3' | ||
const path: Join<[1, 2, 3], '.'> = [1, 2, 3].join('.'); | ||
``` | ||
@category Template Literals | ||
*/ | ||
export type Join< | ||
Strings extends string[], | ||
Strings extends Array<string | number>, | ||
Delimiter extends string, | ||
> = Strings extends [] ? '' : | ||
Strings extends [string] ? `${Strings[0]}` : | ||
Strings extends [string | number] ? `${Strings[0]}` : | ||
// @ts-expect-error `Rest` is inferred as `unknown` here: https://github.com/microsoft/TypeScript/issues/45281 | ||
Strings extends [string, ...infer Rest] ? `${Strings[0]}${Delimiter}${Join<Rest, Delimiter>}` : | ||
string; | ||
Strings extends [string | number, ...infer Rest] ? `${Strings[0]}${Delimiter}${Join<Rest, Delimiter>}` : | ||
string | number; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters