type-fest 0.18.1 → 0.20.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/base.d.ts ADDED
@@ -0,0 +1,38 @@
1
+ // Types that are compatible with all supported TypeScript versions.
2
+ // It's shared between all TypeScript version-specific definitions.
3
+
4
+ // Basic
5
+ export * from './source/basic';
6
+
7
+ // Utilities
8
+ export {Except} from './source/except';
9
+ export {Mutable} from './source/mutable';
10
+ export {Merge} from './source/merge';
11
+ export {MergeExclusive} from './source/merge-exclusive';
12
+ export {RequireAtLeastOne} from './source/require-at-least-one';
13
+ export {RequireExactlyOne} from './source/require-exactly-one';
14
+ export {PartialDeep} from './source/partial-deep';
15
+ export {ReadonlyDeep} from './source/readonly-deep';
16
+ export {LiteralUnion} from './source/literal-union';
17
+ export {Promisable} from './source/promisable';
18
+ export {Opaque} from './source/opaque';
19
+ export {SetOptional} from './source/set-optional';
20
+ export {SetRequired} from './source/set-required';
21
+ export {ValueOf} from './source/value-of';
22
+ export {PromiseValue} from './source/promise-value';
23
+ export {AsyncReturnType} from './source/async-return-type';
24
+ export {ConditionalExcept} from './source/conditional-except';
25
+ export {ConditionalKeys} from './source/conditional-keys';
26
+ export {ConditionalPick} from './source/conditional-pick';
27
+ export {UnionToIntersection} from './source/union-to-intersection';
28
+ export {Stringified} from './source/stringified';
29
+ export {FixedLengthArray} from './source/fixed-length-array';
30
+ export {IterableElement} from './source/iterable-element';
31
+ export {Entry} from './source/entry';
32
+ export {Entries} from './source/entries';
33
+ export {SetReturnType} from './source/set-return-type';
34
+ export {Asyncify} from './source/asyncify';
35
+
36
+ // Miscellaneous
37
+ export {PackageJson} from './source/package-json';
38
+ export {TsConfigJson} from './source/tsconfig-json';
package/index.d.ts CHANGED
@@ -1,35 +1,2 @@
1
- // Basic
2
- export * from './source/basic';
3
-
4
- // Utilities
5
- export {Except} from './source/except';
6
- export {Mutable} from './source/mutable';
7
- export {Merge} from './source/merge';
8
- export {MergeExclusive} from './source/merge-exclusive';
9
- export {RequireAtLeastOne} from './source/require-at-least-one';
10
- export {RequireExactlyOne} from './source/require-exactly-one';
11
- export {PartialDeep} from './source/partial-deep';
12
- export {ReadonlyDeep} from './source/readonly-deep';
13
- export {LiteralUnion} from './source/literal-union';
14
- export {Promisable} from './source/promisable';
15
- export {Opaque} from './source/opaque';
16
- export {SetOptional} from './source/set-optional';
17
- export {SetRequired} from './source/set-required';
18
- export {ValueOf} from './source/value-of';
19
- export {PromiseValue} from './source/promise-value';
20
- export {AsyncReturnType} from './source/async-return-type';
21
- export {ConditionalExcept} from './source/conditional-except';
22
- export {ConditionalKeys} from './source/conditional-keys';
23
- export {ConditionalPick} from './source/conditional-pick';
24
- export {UnionToIntersection} from './source/union-to-intersection';
25
- export {Stringified} from './source/stringified';
26
- export {FixedLengthArray} from './source/fixed-length-array';
27
- export {IterableElement} from './source/iterable-element';
28
- export {Entry} from './source/entry';
29
- export {Entries} from './source/entries';
30
- export {SetReturnType} from './source/set-return-type';
31
- export {Asyncify} from './source/asyncify';
32
-
33
- // Miscellaneous
34
- export {PackageJson} from './source/package-json';
35
- export {TsConfigJson} from './source/tsconfig-json';
1
+ // These are all the basic types that's compatible with all supported TypeScript versions.
2
+ export * from './base';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-fest",
3
- "version": "0.18.1",
3
+ "version": "0.20.2",
4
4
  "description": "A collection of essential TypeScript types",
5
5
  "license": "(MIT OR CC0-1.0)",
6
6
  "repository": "sindresorhus/type-fest",
@@ -14,11 +14,14 @@
14
14
  "node": ">=10"
15
15
  },
16
16
  "scripts": {
17
- "test": "xo && tsd"
17
+ "//test": "xo && tsd && tsc",
18
+ "test": "xo && tsc"
18
19
  },
19
20
  "files": [
20
21
  "index.d.ts",
21
- "source"
22
+ "base.d.ts",
23
+ "source",
24
+ "ts41"
22
25
  ],
23
26
  "keywords": [
24
27
  "typescript",
@@ -32,14 +35,24 @@
32
35
  "json"
33
36
  ],
34
37
  "devDependencies": {
38
+ "@sindresorhus/tsconfig": "~0.7.0",
35
39
  "tsd": "^0.13.1",
36
- "xo": "^0.28.2"
40
+ "typescript": "^4.1.2",
41
+ "xo": "^0.35.0"
42
+ },
43
+ "types": "./index.d.ts",
44
+ "typesVersions": {
45
+ ">=4.1": {
46
+ "*": [
47
+ "ts41/*"
48
+ ]
49
+ }
37
50
  },
38
- "types": "index.d.ts",
39
51
  "xo": {
40
52
  "rules": {
53
+ "@typescript-eslint/ban-types": "off",
41
54
  "@typescript-eslint/indent": "off",
42
- "func-call-spacing": "off"
55
+ "node/no-unsupported-features/es-builtins": "off"
43
56
  }
44
57
  }
45
58
  }
package/readme.md CHANGED
@@ -11,7 +11,6 @@
11
11
  <br>
12
12
  <br>
13
13
 
14
- [![Build Status](https://github.com/sindresorhus/type-fest/workflows/CI/badge.svg?branch=master)](https://github.com/sindresorhus/type-fest/actions?query=branch%3Amaster+workflow%3ACI)
15
14
  [![](https://img.shields.io/badge/unicorn-approved-ff69b4.svg)](https://giphy.com/gifs/illustration-rainbow-unicorn-26AHG5KGFxSkUWw1i)
16
15
  <!-- Commented out until they actually show anything
17
16
  [![npm dependents](https://badgen.net/npm/dependents/type-fest)](https://www.npmjs.com/package/type-fest?activeTab=dependents) [![npm downloads](https://badgen.net/npm/dt/type-fest)](https://www.npmjs.com/package/type-fest)
@@ -29,7 +28,7 @@ PR welcome for additional commonly needed types and docs improvements. Read the
29
28
  $ npm install type-fest
30
29
  ```
31
30
 
32
- *Requires TypeScript >=3.2*
31
+ *Requires TypeScript >=3.4*
33
32
 
34
33
  ## Usage
35
34
 
@@ -89,6 +88,16 @@ Click the type names for complete docs.
89
88
  - [`SetReturnType`](source/set-return-type.d.ts) - Create a function type with a return type of your choice and the same parameters as the given function type.
90
89
  - [`Asyncify`](source/asyncify.d.ts) - Create an async version of the given function type.
91
90
 
91
+ ### Template literal types
92
+
93
+ *Note:* These require [TypeScript 4.1 or newer](https://devblogs.microsoft.com/typescript/announcing-typescript-4-1/#template-literal-types).
94
+
95
+ - [`CamelCase`](ts41/camel-case.d.ts) – Convert a string literal to camel-case (`fooBar`).
96
+ - [`KebabCase`](ts41/kebab-case.d.ts) – Convert a string literal to kebab-case (`foo-bar`).
97
+ - [`PascalCase`](ts41/pascal-case.d.ts) – Converts a string literal to pascal-case (`FooBar`)
98
+ - [`SnakeCase`](ts41/snake-case.d.ts) – Convert a string literal to snake-case (`foo_bar`).
99
+ - [`DelimiterCase`](ts41/delimiter-case.d.ts) – Convert a string literal to a custom string delimiter casing.
100
+
92
101
  ### Miscellaneous
93
102
 
94
103
  - [`PackageJson`](source/package-json.d.ts) - Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-json-file).
@@ -630,6 +639,7 @@ You can find some examples in the [TypeScript docs](https://www.typescriptlang.o
630
639
  - [Sindre Sorhus](https://github.com/sindresorhus)
631
640
  - [Jarek Radosz](https://github.com/CvX)
632
641
  - [Dimitri Benin](https://github.com/BendingBender)
642
+ - [Pelle Wessman](https://github.com/voxpelli)
633
643
 
634
644
  ## License
635
645
 
@@ -200,16 +200,12 @@ declare namespace PackageJson {
200
200
  Run with the `npm restart` command, after `restart`. Note: `npm restart` will run the `stop` and `start` scripts if no `restart` script is provided.
201
201
  */
202
202
  postrestart?: string;
203
- } & {
204
- [scriptName: string]: string;
205
- };
203
+ } & Record<string, string>;
206
204
 
207
205
  /**
208
206
  Dependencies of the package. The version range is a string which has one or more space-separated descriptors. Dependencies can also be identified with a tarball or Git URL.
209
207
  */
210
- export interface Dependency {
211
- [packageName: string]: string;
212
- }
208
+ export type Dependency = Record<string, string>;
213
209
 
214
210
  /**
215
211
  Conditions which provide a way to resolve a package entry point based on the environment.
@@ -232,7 +228,7 @@ declare namespace PackageJson {
232
228
  export type Exports =
233
229
  | string
234
230
  | {[key in ExportCondition]: Exports}
235
- | {[key: string]: Exports};
231
+ | {[key: string]: Exports}; // eslint-disable-line @typescript-eslint/consistent-indexed-object-style
236
232
 
237
233
  export interface NonStandardEntryPoints {
238
234
  /**
@@ -256,9 +252,7 @@ declare namespace PackageJson {
256
252
  */
257
253
  browser?:
258
254
  | string
259
- | {
260
- [moduleName: string]: string | false;
261
- };
255
+ | Record<string, string | false>;
262
256
 
263
257
  /**
264
258
  Denote which files in your project are "pure" and therefore safe for Webpack to prune if unused.
@@ -426,9 +420,7 @@ declare namespace PackageJson {
426
420
  */
427
421
  bin?:
428
422
  | string
429
- | {
430
- [binary: string]: string;
431
- };
423
+ | Record<string, string>;
432
424
 
433
425
  /**
434
426
  Filenames to put in place for the `man` program to find.
@@ -465,9 +457,7 @@ declare namespace PackageJson {
465
457
  /**
466
458
  Is used to set configuration parameters used in package scripts that persist across upgrades.
467
459
  */
468
- config?: {
469
- [configKey: string]: unknown;
470
- };
460
+ config?: Record<string, unknown>;
471
461
 
472
462
  /**
473
463
  The dependencies of the package.
@@ -492,11 +482,7 @@ declare namespace PackageJson {
492
482
  /**
493
483
  Indicate peer dependencies that are optional.
494
484
  */
495
- peerDependenciesMeta?: {
496
- [packageName: string]: {
497
- optional: true;
498
- };
499
- };
485
+ peerDependenciesMeta?: Record<string, {optional: true}>;
500
486
 
501
487
  /**
502
488
  Package names that are bundled when the package is published.
@@ -585,9 +571,7 @@ declare namespace PackageJson {
585
571
  /**
586
572
  A set of config values that will be used at publish-time. It's especially handy to set the tag, registry or access, to ensure that a given package is not tagged with 'latest', published to the global public registry or that a scoped module is private by default.
587
573
  */
588
- publishConfig?: {
589
- [config: string]: unknown;
590
- };
574
+ publishConfig?: Record<string, unknown>;
591
575
 
592
576
  /**
593
577
  Describes and notifies consumers of a package's monetary support information.
@@ -20,13 +20,14 @@ const responder: RequireAtLeastOne<Responder, 'text' | 'json'> = {
20
20
  };
21
21
  ```
22
22
  */
23
- export type RequireAtLeastOne<ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> =
24
- {
25
- // For each Key in KeysType make a mapped type
26
- [Key in KeysType]: (
27
- // …by picking that Key's type and making it required
28
- Required<Pick<ObjectType, Key>>
29
- )
30
- }[KeysType]
31
- // …then, make intersection types by adding the remaining keys to each mapped type.
32
- & Except<ObjectType, KeysType>;
23
+ export type RequireAtLeastOne<
24
+ ObjectType,
25
+ KeysType extends keyof ObjectType = keyof ObjectType
26
+ > = {
27
+ // For each `Key` in `KeysType` make a mapped type:
28
+ [Key in KeysType]-?: Required<Pick<ObjectType, Key>> & // 1. Make `Key`'s type required
29
+ // 2. Make all other keys in `KeysType` optional
30
+ Partial<Pick<ObjectType, Exclude<KeysType, Key>>>;
31
+ }[KeysType] &
32
+ // 3. Add the remaining keys not in `KeysType`
33
+ Except<ObjectType, KeysType>;
@@ -532,9 +532,7 @@ declare namespace TsConfigJson {
532
532
  /**
533
533
  Specify path mapping to be computed relative to baseUrl option.
534
534
  */
535
- paths?: {
536
- [key: string]: string[];
537
- };
535
+ paths?: Record<string, string[]>;
538
536
 
539
537
  /**
540
538
  List of TypeScript language server plugins to load.
@@ -0,0 +1,3 @@
1
+ export type UpperCaseCharacters = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z';
2
+
3
+ export type WordSeparators = '-' | '_' | ' ';
@@ -0,0 +1,72 @@
1
+ import {WordSeparators} from '../source/utilities';
2
+
3
+ /**
4
+ Recursively split a string literal into two parts on the first occurence of the given string, returning an array literal of all the separate parts.
5
+ */
6
+ export type Split<S extends string, D extends string> =
7
+ string extends S ? string[] :
8
+ S extends '' ? [] :
9
+ S extends `${infer T}${D}${infer U}` ? [T, ...Split<U, D>] :
10
+ [S];
11
+
12
+ /**
13
+ Step by step takes the first item in an array literal, formats it and adds it to a string literal, and then recursively appends the remainder.
14
+
15
+ Only to be used by `CamelCaseStringArray<>`.
16
+
17
+ @see CamelCaseStringArray
18
+ */
19
+ type InnerCamelCaseStringArray<Parts extends any[], PreviousPart> =
20
+ Parts extends [`${infer FirstPart}`, ...infer RemainingParts]
21
+ ? FirstPart extends undefined
22
+ ? ''
23
+ : FirstPart extends ''
24
+ ? InnerCamelCaseStringArray<RemainingParts, PreviousPart>
25
+ : `${PreviousPart extends '' ? FirstPart : Capitalize<FirstPart>}${InnerCamelCaseStringArray<RemainingParts, FirstPart>}`
26
+ : '';
27
+
28
+ /**
29
+ Starts fusing the output of `Split<>`, an array literal of strings, into a camel-cased string literal.
30
+
31
+ It's separate from `InnerCamelCaseStringArray<>` to keep a clean API outwards to the rest of the code.
32
+
33
+ @see Split
34
+ */
35
+ type CamelCaseStringArray<Parts extends string[]> =
36
+ Parts extends [`${infer FirstPart}`, ...infer RemainingParts]
37
+ ? Uncapitalize<`${FirstPart}${InnerCamelCaseStringArray<RemainingParts, FirstPart>}`>
38
+ : never;
39
+
40
+ /**
41
+ Convert a string literal to camel-case.
42
+
43
+ This can be useful when, for example, converting some kebab-cased command-line flags or a snake-cased database result.
44
+
45
+ @example
46
+ ```
47
+ import {CamelCase} from 'type-fest';
48
+
49
+ // Simple
50
+
51
+ const someVariable: CamelCase<'foo-bar'> = 'fooBar';
52
+
53
+ // Advanced
54
+
55
+ type CamelCasedProps<T> = {
56
+ [K in keyof T as CamelCase<K>]: T[K]
57
+ };
58
+
59
+ interface RawOptions {
60
+ 'dry-run': boolean;
61
+ 'full_family_name': string;
62
+ foo: number;
63
+ }
64
+
65
+ const dbResult: CamelCasedProps<ModelProps> = {
66
+ dryRun: true,
67
+ fullFamilyName: 'bar.js',
68
+ foo: 123
69
+ };
70
+ ```
71
+ */
72
+ export type CamelCase<K> = K extends string ? CamelCaseStringArray<Split<K, WordSeparators>> : K;
@@ -0,0 +1,85 @@
1
+ import {UpperCaseCharacters, WordSeparators} from '../source/utilities';
2
+
3
+ /**
4
+ Unlike a simpler split, this one includes the delimiter splitted on in the resulting array literal. This is to enable splitting on, for example, upper-case characters.
5
+ */
6
+ export type SplitIncludingDelimiters<Source extends string, Delimiter extends string> =
7
+ Source extends '' ? [] :
8
+ Source extends `${infer FirstPart}${Delimiter}${infer SecondPart}` ?
9
+ (
10
+ Source extends `${FirstPart}${infer UsedDelimiter}${SecondPart}`
11
+ ? UsedDelimiter extends Delimiter
12
+ ? Source extends `${infer FirstPart}${UsedDelimiter}${infer SecondPart}`
13
+ ? [...SplitIncludingDelimiters<FirstPart, Delimiter>, UsedDelimiter, ...SplitIncludingDelimiters<SecondPart, Delimiter>]
14
+ : never
15
+ : never
16
+ : never
17
+ ) :
18
+ [Source];
19
+
20
+ /**
21
+ Format a specific part of the splitted string literal that `StringArrayToDelimiterCase<>` fuses together, ensuring desired casing.
22
+
23
+ @see StringArrayToDelimiterCase
24
+ */
25
+ type StringPartToDelimiterCase<StringPart extends string, UsedWordSeparators extends string, UsedUpperCaseCharacters extends string, Delimiter extends string> =
26
+ StringPart extends UsedWordSeparators ? Delimiter :
27
+ StringPart extends UsedUpperCaseCharacters ? `${Delimiter}${Lowercase<StringPart>}` :
28
+ StringPart;
29
+
30
+ /**
31
+ Takes the result of a splitted string literal and recursively concatenates it together into the desired casing.
32
+
33
+ It receives `UsedWordSeparators` and `UsedUpperCaseCharacters` as input to ensure it's fully encapsulated.
34
+
35
+ @see SplitIncludingDelimiters
36
+ */
37
+ type StringArrayToDelimiterCase<Parts extends any[], UsedWordSeparators extends string, UsedUpperCaseCharacters extends string, Delimiter extends string> =
38
+ Parts extends [`${infer FirstPart}`, ...infer RemainingParts]
39
+ ? `${StringPartToDelimiterCase<FirstPart, UsedWordSeparators, UsedUpperCaseCharacters, Delimiter>}${StringArrayToDelimiterCase<RemainingParts, UsedWordSeparators, UsedUpperCaseCharacters, Delimiter>}`
40
+ : '';
41
+
42
+ /**
43
+ Convert a string literal to a custom string delimiter casing.
44
+
45
+ This can be useful when, for example, converting a camel-cased object property to an oddly cased one.
46
+
47
+ @see KebabCase
48
+ @see SnakeCase
49
+
50
+ @example
51
+ ```
52
+ import {DelimiterCase} from 'type-fest';
53
+
54
+ // Simple
55
+
56
+ const someVariable: DelimiterCase<'fooBar', '#'> = 'foo#bar';
57
+
58
+ // Advanced
59
+
60
+ type OddlyCasedProps<T> = {
61
+ [K in keyof T as DelimiterCase<K, '#'>]: T[K]
62
+ };
63
+
64
+ interface SomeOptions {
65
+ dryRun: boolean;
66
+ includeFile: string;
67
+ foo: number;
68
+ }
69
+
70
+ const rawCliOptions: OddlyCasedProps<SomeOptions> = {
71
+ 'dry#run': true,
72
+ 'include#file': 'bar.js',
73
+ foo: 123
74
+ };
75
+ ```
76
+ */
77
+
78
+ export type DelimiterCase<Value, Delimiter extends string> = Value extends string
79
+ ? StringArrayToDelimiterCase<
80
+ SplitIncludingDelimiters<Value, WordSeparators | UpperCaseCharacters>,
81
+ WordSeparators,
82
+ UpperCaseCharacters,
83
+ Delimiter
84
+ >
85
+ : Value;
@@ -0,0 +1,9 @@
1
+ // These are all the basic types that's compatible with all supported TypeScript versions.
2
+ export * from '../base';
3
+
4
+ // These are special types that require at least TypeScript 4.1.
5
+ export {CamelCase} from './camel-case';
6
+ export {KebabCase} from './kebab-case';
7
+ export {PascalCase} from './pascal-case';
8
+ export {SnakeCase} from './snake-case';
9
+ export {DelimiterCase} from './delimiter-case';
@@ -0,0 +1,36 @@
1
+ import {DelimiterCase} from './delimiter-case';
2
+
3
+ /**
4
+ Convert a string literal to kebab-case.
5
+
6
+ This can be useful when, for example, converting a camel-cased object property to a kebab-cased CSS class name or a command-line flag.
7
+
8
+ @example
9
+ ```
10
+ import {KebabCase} from 'type-fest';
11
+
12
+ // Simple
13
+
14
+ const someVariable: KebabCase<'fooBar'> = 'foo-bar';
15
+
16
+ // Advanced
17
+
18
+ type KebabCasedProps<T> = {
19
+ [K in keyof T as KebabCase<K>]: T[K]
20
+ };
21
+
22
+ interface CliOptions {
23
+ dryRun: boolean;
24
+ includeFile: string;
25
+ foo: number;
26
+ }
27
+
28
+ const rawCliOptions: KebabCasedProps<CliOptions> = {
29
+ 'dry-run': true,
30
+ 'include-file': 'bar.js',
31
+ foo: 123
32
+ };
33
+ ```
34
+ */
35
+
36
+ export type KebabCase<Value> = DelimiterCase<Value, '-'>;
@@ -0,0 +1,36 @@
1
+ import {CamelCase} from './camel-case';
2
+
3
+ /**
4
+ Converts a string literal to pascal-case.
5
+
6
+ @example
7
+ ```
8
+ import {PascalCase} from 'type-fest';
9
+
10
+ // Simple
11
+
12
+ const someVariable: PascalCase<'foo-bar'> = 'FooBar';
13
+
14
+ // Advanced
15
+
16
+ type PascalCaseProps<T> = {
17
+ [K in keyof T as PascalCase<K>]: T[K]
18
+ };
19
+
20
+ interface RawOptions {
21
+ 'dry-run': boolean;
22
+ 'full_family_name': string;
23
+ foo: number;
24
+ }
25
+
26
+ const dbResult: CamelCasedProps<ModelProps> = {
27
+ DryRun: true,
28
+ FullFamilyName: 'bar.js',
29
+ Foo: 123
30
+ };
31
+ ```
32
+ */
33
+
34
+ export type PascalCase<Value> = CamelCase<Value> extends string
35
+ ? Capitalize<CamelCase<Value>>
36
+ : CamelCase<Value>;
@@ -0,0 +1,35 @@
1
+ import {DelimiterCase} from './delimiter-case';
2
+
3
+ /**
4
+ Convert a string literal to snake-case.
5
+
6
+ This can be useful when, for example, converting a camel-cased object property to a snake-cased SQL column name.
7
+
8
+ @example
9
+ ```
10
+ import {SnakeCase} from 'type-fest';
11
+
12
+ // Simple
13
+
14
+ const someVariable: SnakeCase<'fooBar'> = 'foo_bar';
15
+
16
+ // Advanced
17
+
18
+ type SnakeCasedProps<T> = {
19
+ [K in keyof T as SnakeCase<K>]: T[K]
20
+ };
21
+
22
+ interface ModelProps {
23
+ isHappy: boolean;
24
+ fullFamilyName: string;
25
+ foo: number;
26
+ }
27
+
28
+ const dbResult: SnakeCasedProps<ModelProps> = {
29
+ 'is_happy': true,
30
+ 'full_family_name': 'Carla Smith',
31
+ foo: 123
32
+ };
33
+ ```
34
+ */
35
+ export type SnakeCase<Value> = DelimiterCase<Value, '_'>;