type-fest 0.15.0 → 0.18.0

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/index.d.ts CHANGED
@@ -24,6 +24,11 @@ export {ConditionalPick} from './source/conditional-pick';
24
24
  export {UnionToIntersection} from './source/union-to-intersection';
25
25
  export {Stringified} from './source/stringified';
26
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';
27
32
 
28
33
  // Miscellaneous
29
34
  export {PackageJson} from './source/package-json';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-fest",
3
- "version": "0.15.0",
3
+ "version": "0.18.0",
4
4
  "description": "A collection of essential TypeScript types",
5
5
  "license": "(MIT OR CC0-1.0)",
6
6
  "repository": "sindresorhus/type-fest",
@@ -32,7 +32,7 @@
32
32
  "json"
33
33
  ],
34
34
  "devDependencies": {
35
- "tsd": "^0.11.0",
35
+ "tsd": "^0.13.1",
36
36
  "xo": "^0.28.2"
37
37
  },
38
38
  "types": "index.d.ts",
package/readme.md CHANGED
@@ -83,6 +83,11 @@ Click the type names for complete docs.
83
83
  - [`UnionToIntersection`](source/union-to-intersection.d.ts) - Convert a union type to an intersection type.
84
84
  - [`Stringified`](source/stringified.d.ts) - Create a type with the keys of the given type changed to `string` type.
85
85
  - [`FixedLengthArray`](source/fixed-length-array.d.ts) - Create a type that represents an array of the given type and length.
86
+ - [`IterableElement`](source/iterable-element.d.ts) - Get the element type of an `Iterable`/`AsyncIterable`. For example, an array or a generator.
87
+ - [`Entry`](source/entry.d.ts) - Create a type that represents the type of an entry of a collection.
88
+ - [`Entries`](source/entries.d.ts) - Create a type that represents the type of the entries of a collection.
89
+ - [`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
+ - [`Asyncify`](source/asyncify.d.ts) - Create an async version of the given function type.
86
91
 
87
92
  ### Miscellaneous
88
93
 
@@ -0,0 +1,31 @@
1
+ import {PromiseValue} from './promise-value';
2
+ import {SetReturnType} from './set-return-type';
3
+
4
+ /**
5
+ Create an async version of the given function type, by boxing the return type in `Promise` while keeping the same parameter types.
6
+
7
+ Use-case: You have two functions, one synchronous and one asynchronous that do the same thing. Instead of having to duplicate the type definition, you can use `Asyncify` to reuse the synchronous type.
8
+
9
+ @example
10
+ ```
11
+ import {Asyncify} from 'type-fest';
12
+
13
+ // Synchronous function.
14
+ function getFooSync(someArg: SomeType): Foo {
15
+ // …
16
+ }
17
+
18
+ type AsyncifiedFooGetter = Asyncify<typeof getFooSync>;
19
+ //=> type AsyncifiedFooGetter = (someArg: SomeType) => Promise<Foo>;
20
+
21
+ // Same as `getFooSync` but asynchronous.
22
+ const getFooAsync: AsyncifiedFooGetter = (someArg) => {
23
+ // TypeScript now knows that `someArg` is `SomeType` automatically.
24
+ // It also knows that this function must return `Promise<Foo>`.
25
+ // If you have `@typescript-eslint/promise-function-async` linter rule enabled, it will even report that "Functions that return promises must be async.".
26
+
27
+ // …
28
+ }
29
+ ```
30
+ */
31
+ export type Asyncify<Fn extends (...args: any[]) => any> = SetReturnType<Fn, Promise<PromiseValue<ReturnType<Fn>>>>;
@@ -0,0 +1,57 @@
1
+ import {ArrayEntry, MapEntry, ObjectEntry, SetEntry} from './entry';
2
+
3
+ type ArrayEntries<BaseType extends readonly unknown[]> = Array<ArrayEntry<BaseType>>;
4
+ type MapEntries<BaseType> = Array<MapEntry<BaseType>>;
5
+ type ObjectEntries<BaseType> = Array<ObjectEntry<BaseType>>;
6
+ type SetEntries<BaseType extends Set<unknown>> = Array<SetEntry<BaseType>>;
7
+
8
+ /**
9
+ Many collections have an `entries` method which returns an array of a given object's own enumerable string-keyed property [key, value] pairs. The `Entries` type will return the type of that collection's entries.
10
+
11
+ For example the {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries|`Object`}, {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/entries|`Map`}, {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries|`Array`}, and {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/entries|`Set`} collections all have this method. Note that `WeakMap` and `WeakSet` do not have this method since their entries are not enumerable.
12
+
13
+ @see `Entry` if you want to just access the type of a single entry.
14
+
15
+ @example
16
+ ```
17
+ import {Entries} from 'type-fest';
18
+
19
+ interface Example {
20
+ someKey: number;
21
+ }
22
+
23
+ const manipulatesEntries = (examples: Entries<Example>) => examples.map(example => [
24
+ // Does some arbitrary processing on the key (with type information available)
25
+ example[0].toUpperCase(),
26
+
27
+ // Does some arbitrary processing on the value (with type information available)
28
+ example[1].toFixed()
29
+ ]);
30
+
31
+ const example: Example = {someKey: 1};
32
+ const entries = Object.entries(example) as Entries<Example>;
33
+ const output = manipulatesEntries(entries);
34
+
35
+ // Objects
36
+ const objectExample = {a: 1};
37
+ const objectEntries: Entries<typeof objectExample> = [['a', 1]];
38
+
39
+ // Arrays
40
+ const arrayExample = ['a', 1];
41
+ const arrayEntries: Entries<typeof arrayExample> = [[0, 'a'], [1, 1]];
42
+
43
+ // Maps
44
+ const mapExample = new Map([['a', 1]]);
45
+ const mapEntries: Entries<typeof map> = [['a', 1]];
46
+
47
+ // Sets
48
+ const setExample = new Set(['a', 1]);
49
+ const setEntries: Entries<typeof setExample> = [['a', 'a'], [1, 1]];
50
+ ```
51
+ */
52
+ export type Entries<BaseType> =
53
+ BaseType extends Map<unknown, unknown> ? MapEntries<BaseType>
54
+ : BaseType extends Set<unknown> ? SetEntries<BaseType>
55
+ : BaseType extends unknown[] ? ArrayEntries<BaseType>
56
+ : BaseType extends object ? ObjectEntries<BaseType>
57
+ : never;
@@ -0,0 +1,60 @@
1
+ type MapKey<BaseType> = BaseType extends Map<infer KeyType, unknown> ? KeyType : never;
2
+ type MapValue<BaseType> = BaseType extends Map<unknown, infer ValueType> ? ValueType : never;
3
+
4
+ type ArrayEntry<BaseType extends readonly unknown[]> = [number, BaseType[number]];
5
+ type MapEntry<BaseType> = [MapKey<BaseType>, MapValue<BaseType>];
6
+ type ObjectEntry<BaseType> = [keyof BaseType, BaseType[keyof BaseType]];
7
+ type SetEntry<BaseType> = BaseType extends Set<infer ItemType> ? [ItemType, ItemType] : never;
8
+
9
+ /**
10
+ Many collections have an `entries` method which returns an array of a given object's own enumerable string-keyed property [key, value] pairs. The `Entry` type will return the type of that collection's entry.
11
+
12
+ For example the {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries|`Object`}, {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/entries|`Map`}, {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries|`Array`}, and {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/entries|`Set`} collections all have this method. Note that `WeakMap` and `WeakSet` do not have this method since their entries are not enumerable.
13
+
14
+ @see `Entries` if you want to just access the type of the array of entries (which is the return of the `.entries()` method).
15
+
16
+ @example
17
+ ```
18
+ import {Entry} from 'type-fest';
19
+
20
+ interface Example {
21
+ someKey: number;
22
+ }
23
+
24
+ const manipulatesEntry = (example: Entry<Example>) => [
25
+ // Does some arbitrary processing on the key (with type information available)
26
+ example[0].toUpperCase(),
27
+
28
+ // Does some arbitrary processing on the value (with type information available)
29
+ example[1].toFixed(),
30
+ ];
31
+
32
+ const example: Example = {someKey: 1};
33
+ const entry = Object.entries(example)[0] as Entry<Example>;
34
+ const output = manipulatesEntry(entry);
35
+
36
+ // Objects
37
+ const objectExample = {a: 1};
38
+ const objectEntry: Entry<typeof objectExample> = ['a', 1];
39
+
40
+ // Arrays
41
+ const arrayExample = ['a', 1];
42
+ const arrayEntryString: Entry<typeof arrayExample> = [0, 'a'];
43
+ const arrayEntryNumber: Entry<typeof arrayExample> = [1, 1];
44
+
45
+ // Maps
46
+ const mapExample = new Map([['a', 1]]);
47
+ const mapEntry: Entry<typeof map> = ['a', 1];
48
+
49
+ // Sets
50
+ const setExample = new Set(['a', 1]);
51
+ const setEntryString: Entry<typeof setExample> = ['a', 'a'];
52
+ const setEntryNumber: Entry<typeof setExample> = [1, 1];
53
+ ```
54
+ */
55
+ export type Entry<BaseType> =
56
+ BaseType extends Map<unknown, unknown> ? MapEntry<BaseType>
57
+ : BaseType extends Set<unknown> ? SetEntry<BaseType>
58
+ : BaseType extends unknown[] ? ArrayEntry<BaseType>
59
+ : BaseType extends object ? ObjectEntry<BaseType>
60
+ : never;
@@ -0,0 +1,46 @@
1
+ /**
2
+ Get the element type of an `Iterable`/`AsyncIterable`. For example, an array or a generator.
3
+
4
+ This can be useful, for example, if you want to get the type that is yielded in a generator function. Often the return type of those functions are not specified.
5
+
6
+ This type works with both `Iterable`s and `AsyncIterable`s, so it can be use with synchronous and asynchronous generators.
7
+
8
+ Here is an example of `IterableElement` in action with a generator function:
9
+
10
+ @example
11
+ ```
12
+ function * iAmGenerator() {
13
+ yield 1;
14
+ yield 2;
15
+ }
16
+
17
+ type MeNumber = IterableElement<ReturnType<typeof iAmGenerator>>
18
+ ```
19
+
20
+ And here is an example with an async generator:
21
+
22
+ @example
23
+ ```
24
+ async function * iAmGeneratorAsync() {
25
+ yield 'hi';
26
+ yield true;
27
+ }
28
+
29
+ type MeStringOrBoolean = IterableElement<ReturnType<typeof iAmGeneratorAsync>>
30
+ ```
31
+
32
+ Many types in JavaScript/TypeScript are iterables. This type works on all types that implement those interfaces. For example, `Array`, `Set`, `Map`, `stream.Readable`, etc.
33
+
34
+ An example with an array of strings:
35
+
36
+ @example
37
+ ```
38
+ type MeString = IterableElement<string[]>
39
+ ```
40
+ */
41
+ export type IterableElement<TargetIterable> =
42
+ TargetIterable extends Iterable<infer ElementType> ?
43
+ ElementType :
44
+ TargetIterable extends AsyncIterable<infer ElementType> ?
45
+ ElementType :
46
+ never;
@@ -28,6 +28,6 @@ const pet: Pet2 = '';
28
28
  ```
29
29
  */
30
30
  export type LiteralUnion<
31
- LiteralType extends BaseType,
31
+ LiteralType,
32
32
  BaseType extends Primitive
33
33
  > = LiteralType | (BaseType & {_?: never});
@@ -1,4 +1,4 @@
1
- import {LiteralUnion} from '..';
1
+ import {LiteralUnion} from './literal-union';
2
2
 
3
3
  declare namespace PackageJson {
4
4
  /**
@@ -337,194 +337,193 @@ declare namespace PackageJson {
337
337
  */
338
338
  jspm?: PackageJson;
339
339
  }
340
- }
341
-
342
- /**
343
- Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-json-file). Also includes types for fields used by other popular projects, like TypeScript and Yarn.
344
- */
345
- export type PackageJson = {
346
- /**
347
- The name of the package.
348
- */
349
- name?: string;
350
-
351
- /**
352
- Package version, parseable by [`node-semver`](https://github.com/npm/node-semver).
353
- */
354
- version?: string;
355
340
 
356
341
  /**
357
- Package description, listed in `npm search`.
342
+ Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-json-file). Containing standard npm properties.
358
343
  */
359
- description?: string;
344
+ export interface PackageJsonStandard {
345
+ /**
346
+ The name of the package.
347
+ */
348
+ name?: string;
360
349
 
361
- /**
362
- Keywords associated with package, listed in `npm search`.
363
- */
364
- keywords?: string[];
350
+ /**
351
+ Package version, parseable by [`node-semver`](https://github.com/npm/node-semver).
352
+ */
353
+ version?: string;
365
354
 
366
- /**
367
- The URL to the package's homepage.
368
- */
369
- homepage?: LiteralUnion<'.', string>;
355
+ /**
356
+ Package description, listed in `npm search`.
357
+ */
358
+ description?: string;
370
359
 
371
- /**
372
- The URL to the package's issue tracker and/or the email address to which issues should be reported.
373
- */
374
- bugs?: PackageJson.BugsLocation;
360
+ /**
361
+ Keywords associated with package, listed in `npm search`.
362
+ */
363
+ keywords?: string[];
375
364
 
376
- /**
377
- The license for the package.
378
- */
379
- license?: string;
365
+ /**
366
+ The URL to the package's homepage.
367
+ */
368
+ homepage?: LiteralUnion<'.', string>;
380
369
 
381
- /**
382
- The licenses for the package.
383
- */
384
- licenses?: Array<{
385
- type?: string;
386
- url?: string;
387
- }>;
370
+ /**
371
+ The URL to the package's issue tracker and/or the email address to which issues should be reported.
372
+ */
373
+ bugs?: BugsLocation;
388
374
 
389
- author?: PackageJson.Person;
375
+ /**
376
+ The license for the package.
377
+ */
378
+ license?: string;
390
379
 
391
- /**
392
- A list of people who contributed to the package.
393
- */
394
- contributors?: PackageJson.Person[];
380
+ /**
381
+ The licenses for the package.
382
+ */
383
+ licenses?: Array<{
384
+ type?: string;
385
+ url?: string;
386
+ }>;
395
387
 
396
- /**
397
- A list of people who maintain the package.
398
- */
399
- maintainers?: PackageJson.Person[];
388
+ author?: Person;
400
389
 
401
- /**
402
- The files included in the package.
403
- */
404
- files?: string[];
390
+ /**
391
+ A list of people who contributed to the package.
392
+ */
393
+ contributors?: Person[];
405
394
 
406
- /**
407
- Resolution algorithm for importing ".js" files from the package's scope.
395
+ /**
396
+ A list of people who maintain the package.
397
+ */
398
+ maintainers?: Person[];
408
399
 
409
- [Read more.](https://nodejs.org/api/esm.html#esm_package_json_type_field)
410
- */
411
- type?: 'module' | 'commonjs';
400
+ /**
401
+ The files included in the package.
402
+ */
403
+ files?: string[];
412
404
 
413
- /**
414
- The module ID that is the primary entry point to the program.
415
- */
416
- main?: string;
405
+ /**
406
+ Resolution algorithm for importing ".js" files from the package's scope.
417
407
 
418
- /**
419
- Standard entry points of the package, with enhanced support for ECMAScript Modules.
408
+ [Read more.](https://nodejs.org/api/esm.html#esm_package_json_type_field)
409
+ */
410
+ type?: 'module' | 'commonjs';
420
411
 
421
- [Read more.](https://nodejs.org/api/esm.html#esm_package_entry_points)
422
- */
423
- exports?: PackageJson.Exports;
412
+ /**
413
+ The module ID that is the primary entry point to the program.
414
+ */
415
+ main?: string;
424
416
 
425
- /**
426
- The executable files that should be installed into the `PATH`.
427
- */
428
- bin?:
429
- | string
430
- | {
431
- [binary: string]: string;
432
- };
417
+ /**
418
+ Standard entry points of the package, with enhanced support for ECMAScript Modules.
433
419
 
434
- /**
435
- Filenames to put in place for the `man` program to find.
436
- */
437
- man?: string | string[];
420
+ [Read more.](https://nodejs.org/api/esm.html#esm_package_entry_points)
421
+ */
422
+ exports?: Exports;
438
423
 
439
- /**
440
- Indicates the structure of the package.
441
- */
442
- directories?: PackageJson.DirectoryLocations;
424
+ /**
425
+ The executable files that should be installed into the `PATH`.
426
+ */
427
+ bin?:
428
+ | string
429
+ | {
430
+ [binary: string]: string;
431
+ };
443
432
 
444
- /**
445
- Location for the code repository.
446
- */
447
- repository?:
448
- | string
449
- | {
450
- type: string;
451
- url: string;
433
+ /**
434
+ Filenames to put in place for the `man` program to find.
435
+ */
436
+ man?: string | string[];
452
437
 
453
438
  /**
454
- Relative path to package.json if it is placed in non-root directory (for example if it is part of a monorepo).
439
+ Indicates the structure of the package.
440
+ */
441
+ directories?: DirectoryLocations;
455
442
 
456
- [Read more.](https://github.com/npm/rfcs/blob/latest/implemented/0010-monorepo-subdirectory-declaration.md)
443
+ /**
444
+ Location for the code repository.
457
445
  */
458
- directory?: string;
459
- };
446
+ repository?:
447
+ | string
448
+ | {
449
+ type: string;
450
+ url: string;
460
451
 
461
- /**
462
- Script commands that are run at various times in the lifecycle of the package. The key is the lifecycle event, and the value is the command to run at that point.
463
- */
464
- scripts?: PackageJson.Scripts;
452
+ /**
453
+ Relative path to package.json if it is placed in non-root directory (for example if it is part of a monorepo).
465
454
 
466
- /**
467
- Is used to set configuration parameters used in package scripts that persist across upgrades.
468
- */
469
- config?: {
470
- [configKey: string]: unknown;
471
- };
455
+ [Read more.](https://github.com/npm/rfcs/blob/latest/implemented/0010-monorepo-subdirectory-declaration.md)
456
+ */
457
+ directory?: string;
458
+ };
472
459
 
473
- /**
474
- The dependencies of the package.
475
- */
476
- dependencies?: PackageJson.Dependency;
460
+ /**
461
+ Script commands that are run at various times in the lifecycle of the package. The key is the lifecycle event, and the value is the command to run at that point.
462
+ */
463
+ scripts?: Scripts;
477
464
 
478
- /**
479
- Additional tooling dependencies that are not required for the package to work. Usually test, build, or documentation tooling.
480
- */
481
- devDependencies?: PackageJson.Dependency;
465
+ /**
466
+ Is used to set configuration parameters used in package scripts that persist across upgrades.
467
+ */
468
+ config?: {
469
+ [configKey: string]: unknown;
470
+ };
482
471
 
483
- /**
484
- Dependencies that are skipped if they fail to install.
485
- */
486
- optionalDependencies?: PackageJson.Dependency;
472
+ /**
473
+ The dependencies of the package.
474
+ */
475
+ dependencies?: Dependency;
487
476
 
488
- /**
489
- Dependencies that will usually be required by the package user directly or via another dependency.
490
- */
491
- peerDependencies?: PackageJson.Dependency;
477
+ /**
478
+ Additional tooling dependencies that are not required for the package to work. Usually test, build, or documentation tooling.
479
+ */
480
+ devDependencies?: Dependency;
492
481
 
493
- /**
494
- Indicate peer dependencies that are optional.
495
- */
496
- peerDependenciesMeta?: {
497
- [packageName: string]: {
498
- optional: true;
482
+ /**
483
+ Dependencies that are skipped if they fail to install.
484
+ */
485
+ optionalDependencies?: Dependency;
486
+
487
+ /**
488
+ Dependencies that will usually be required by the package user directly or via another dependency.
489
+ */
490
+ peerDependencies?: Dependency;
491
+
492
+ /**
493
+ Indicate peer dependencies that are optional.
494
+ */
495
+ peerDependenciesMeta?: {
496
+ [packageName: string]: {
497
+ optional: true;
498
+ };
499
499
  };
500
- };
501
500
 
502
- /**
503
- Package names that are bundled when the package is published.
504
- */
505
- bundledDependencies?: string[];
501
+ /**
502
+ Package names that are bundled when the package is published.
503
+ */
504
+ bundledDependencies?: string[];
506
505
 
507
- /**
508
- Alias of `bundledDependencies`.
509
- */
510
- bundleDependencies?: string[];
506
+ /**
507
+ Alias of `bundledDependencies`.
508
+ */
509
+ bundleDependencies?: string[];
511
510
 
512
- /**
513
- Engines that this package runs on.
514
- */
515
- engines?: {
516
- [EngineName in 'npm' | 'node' | string]: string;
517
- };
511
+ /**
512
+ Engines that this package runs on.
513
+ */
514
+ engines?: {
515
+ [EngineName in 'npm' | 'node' | string]: string;
516
+ };
518
517
 
519
- /**
520
- @deprecated
521
- */
522
- engineStrict?: boolean;
518
+ /**
519
+ @deprecated
520
+ */
521
+ engineStrict?: boolean;
523
522
 
524
- /**
525
- Operating systems the module runs on.
526
- */
527
- os?: Array<LiteralUnion<
523
+ /**
524
+ Operating systems the module runs on.
525
+ */
526
+ os?: Array<LiteralUnion<
528
527
  | 'aix'
529
528
  | 'darwin'
530
529
  | 'freebsd'
@@ -540,12 +539,12 @@ export type PackageJson = {
540
539
  | '!sunos'
541
540
  | '!win32',
542
541
  string
543
- >>;
542
+ >>;
544
543
 
545
- /**
546
- CPU architectures the module runs on.
547
- */
548
- cpu?: Array<LiteralUnion<
544
+ /**
545
+ CPU architectures the module runs on.
546
+ */
547
+ cpu?: Array<LiteralUnion<
549
548
  | 'arm'
550
549
  | 'arm64'
551
550
  | 'ia32'
@@ -569,37 +568,37 @@ export type PackageJson = {
569
568
  | '!x32'
570
569
  | '!x64',
571
570
  string
572
- >>;
571
+ >>;
573
572
 
574
- /**
575
- If set to `true`, a warning will be shown if package is installed locally. Useful if the package is primarily a command-line application that should be installed globally.
576
-
577
- @deprecated
578
- */
579
- preferGlobal?: boolean;
573
+ /**
574
+ If set to `true`, a warning will be shown if package is installed locally. Useful if the package is primarily a command-line application that should be installed globally.
580
575
 
581
- /**
582
- If set to `true`, then npm will refuse to publish it.
583
- */
584
- private?: boolean;
576
+ @deprecated
577
+ */
578
+ preferGlobal?: boolean;
585
579
 
586
- /**
587
- 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.
588
- */
589
- publishConfig?: {
590
- [config: string]: unknown;
591
- };
580
+ /**
581
+ If set to `true`, then npm will refuse to publish it.
582
+ */
583
+ private?: boolean;
592
584
 
593
- /**
594
- Describes and notifies consumers of a package's monetary support information.
585
+ /**
586
+ 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
+ */
588
+ publishConfig?: {
589
+ [config: string]: unknown;
590
+ };
595
591
 
596
- [Read more.](https://github.com/npm/rfcs/blob/latest/accepted/0017-add-funding-support.md)
597
- */
598
- funding?: string | {
599
592
  /**
600
- The type of funding.
593
+ Describes and notifies consumers of a package's monetary support information.
594
+
595
+ [Read more.](https://github.com/npm/rfcs/blob/latest/accepted/0017-add-funding-support.md)
601
596
  */
602
- type?: LiteralUnion<
597
+ funding?: string | {
598
+ /**
599
+ The type of funding.
600
+ */
601
+ type?: LiteralUnion<
603
602
  | 'github'
604
603
  | 'opencollective'
605
604
  | 'patreon'
@@ -607,16 +606,22 @@ export type PackageJson = {
607
606
  | 'foundation'
608
607
  | 'corporation',
609
608
  string
610
- >;
611
- /**
612
- The URL to the funding page.
613
- */
614
- url: string;
615
- };
616
- } &
609
+ >;
610
+
611
+ /**
612
+ The URL to the funding page.
613
+ */
614
+ url: string;
615
+ };
616
+ }
617
+ }
618
+
619
+ /**
620
+ Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-json-file). Also includes types for fields used by other popular projects, like TypeScript and Yarn.
621
+ */
622
+ export type PackageJson =
623
+ PackageJson.PackageJsonStandard &
617
624
  PackageJson.NonStandardEntryPoints &
618
625
  PackageJson.TypeScriptConfiguration &
619
626
  PackageJson.YarnConfiguration &
620
- PackageJson.JSPMConfiguration & {
621
- [key: string]: unknown;
622
- };
627
+ PackageJson.JSPMConfiguration;
@@ -1,5 +1,6 @@
1
1
  /**
2
2
  Returns the type that is wrapped inside a `Promise` type.
3
+ If the type is a nested Promise, it is unwrapped recursively until a non-Promise type is obtained.
3
4
  If the type is not a `Promise`, the type itself is returned.
4
5
 
5
6
  @example
@@ -15,6 +16,12 @@ let data: Data = await asyncData;
15
16
  // Here's an example that shows how this type reacts to non-Promise types.
16
17
  type SyncData = PromiseValue<string>;
17
18
  let syncData: SyncData = getSyncData();
19
+
20
+ // Here's an example that shows how this type reacts to recursive Promise types.
21
+ type RecursiveAsyncData = Promise<Promise<string> >;
22
+ let recursiveAsyncData: PromiseValue<RecursiveAsyncData> = Promise.resolve(Promise.resolve('ABC'));
18
23
  ```
19
24
  */
20
- export type PromiseValue<PromiseType, Otherwise = PromiseType> = PromiseType extends Promise<infer Value> ? Value : Otherwise;
25
+ export type PromiseValue<PromiseType, Otherwise = PromiseType> = PromiseType extends Promise<infer Value>
26
+ ? { 0: PromiseValue<Value>; 1: Value }[PromiseType extends Promise<unknown> ? 0 : 1]
27
+ : Otherwise;
@@ -0,0 +1,29 @@
1
+ type IsAny<T> = 0 extends (1 & T) ? true : false; // https://stackoverflow.com/a/49928360/3406963
2
+ type IsNever<T> = [T] extends [never] ? true : false;
3
+ type IsUnknown<T> = IsNever<T> extends false ? T extends unknown ? unknown extends T ? IsAny<T> extends false ? true : false : false : false : false;
4
+
5
+ /**
6
+ Create a function type with a return type of your choice and the same parameters as the given function type.
7
+
8
+ Use-case: You want to define a wrapped function that returns something different while receiving the same parameters. For example, you might want to wrap a function that can throw an error into one that will return `undefined` instead.
9
+
10
+ @example
11
+ ```
12
+ import {SetReturnType} from 'type-fest';
13
+
14
+ type MyFunctionThatCanThrow = (foo: SomeType, bar: unknown) => SomeOtherType;
15
+
16
+ type MyWrappedFunction = SetReturnType<MyFunctionThatCanThrow, SomeOtherType | undefined>;
17
+ //=> type MyWrappedFunction = (foo: SomeType, bar: unknown) => SomeOtherType | undefined;
18
+ ```
19
+ */
20
+ export type SetReturnType<Fn extends (...args: any[]) => any, TypeToReturn> =
21
+ // Just using `Parameters<Fn>` isn't ideal because it doesn't handle the `this` fake parameter.
22
+ Fn extends (this: infer ThisArg, ...args: infer Arguments) => any ? (
23
+ // If a function did not specify the `this` fake parameter, it will be inferred to `unknown`.
24
+ // We want to detect this situation just to display a friendlier type upon hovering on an IntelliSense-powered IDE.
25
+ IsUnknown<ThisArg> extends true ? (...args: Arguments) => TypeToReturn : (this: ThisArg, ...args: Arguments) => TypeToReturn
26
+ ) : (
27
+ // This part should be unreachable, but we make it meaningful just in case…
28
+ (...args: Parameters<Fn>) => TypeToReturn
29
+ );