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

Support numbers in Join type #258

Merged
merged 6 commits into from Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -143,7 +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.
- [`Join`](source/join.d.ts) - Join an array of strings and/or numbers using the given string as a 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
12 changes: 6 additions & 6 deletions source/join.d.ts
@@ -1,22 +1,22 @@
/**
Join an array of strings using the given string as delimiter.
Join an array of strings and/or numbers using the given string as delimiter.
o-alexandrov marked this conversation as resolved.
Show resolved Hide resolved

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('.');
const path: Join<['foo', '0', 'baz'], '.'> = ['foo', '0', 'baz'].join('.');
o-alexandrov marked this conversation as resolved.
Show resolved Hide resolved
```

@category Template Literals
*/
export type Join<
Strings extends string[],
Strings extends (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;
2 changes: 2 additions & 0 deletions test-d/join.ts
Expand Up @@ -3,7 +3,9 @@ import {Join} from '../index';

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