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

Add Join type #235

Merged
merged 1 commit into from Sep 5, 2021
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
1 change: 1 addition & 0 deletions index.d.ts
Expand Up @@ -52,6 +52,7 @@ export {ScreamingSnakeCase} from './source/screaming-snake-case';
export {DelimiterCase} from './source/delimiter-case';
export {DelimiterCasedProperties} from './source/delimiter-cased-properties';
export {DelimiterCasedPropertiesDeep} from './source/delimiter-cased-properties-deep';
export {Join} from './source/join';
export {Split} from './source/split';
export {Trim} from './source/trim';
export {Includes} from './source/includes';
Expand Down
1 change: 1 addition & 0 deletions readme.md
Expand Up @@ -143,6 +143,7 @@ Click the type names for complete docs.
- [`DelimiterCase`](source/delimiter-case.d.ts) – Convert a string literal to a custom string delimiter casing.
- [`DelimiterCasedProperties`](source/delimiter-cased-properties.d.ts) – Convert object properties to a custom string delimiter casing.
- [`DelimiterCasedPropertiesDeep`](source/delimiter-cased-properties-deep.d.ts) – Convert object properties to a custom string delimiter casing recursively.
- [`Join`](source/join.d.ts) - Join an array of strings using the given string as delimiter.
- [`Split`](source/split.d.ts) - Represents an array of strings split using a given character or character set.
- [`Trim`](source/trim.d.ts) - Remove leading and trailing spaces from a string.
- [`Get`](source/get.d.ts) - Get a deeply-nested property from an object using a key path, like [Lodash's `.get()`](https://lodash.com/docs/latest#get) function.
Expand Down
22 changes: 22 additions & 0 deletions source/join.d.ts
@@ -0,0 +1,22 @@
/**
Join an array of strings using the given string as 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';

const path: Join<['foo', 'bar', 'baz'], '.'> = ['foo', 'bar', 'baz'].join('.');
```

@category Template Literals
*/
export type Join<
Strings extends string[],
Delimiter extends string,
> = Strings extends [] ? '' :
Strings extends [string] ? `${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;
19 changes: 19 additions & 0 deletions test-d/join.ts
@@ -0,0 +1,19 @@
import {expectError, expectType} from 'tsd';
import {Join} from '../index';

// General use.
const generalTest: Join<['foo', 'bar', 'baz'], '.'> = 'foo.bar.baz';
expectType<'foo.bar.baz'>(generalTest);
expectError<'foo'>(generalTest);
expectError<'foo.bar'>(generalTest);
expectError<'foo.bar.ham'>(generalTest);

// Empty string delimiter.
const emptyDelimiter: Join<['foo', 'bar', 'baz'], ''> = 'foobarbaz';
expectType<'foobarbaz'>(emptyDelimiter);
expectError<'foo.bar.baz'>(emptyDelimiter);

// Empty input.
const emptyInput: Join<[], '.'> = '';
expectType<''>(emptyInput);
expectError<'foo'>(emptyInput);