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 +5 -0
- package/package.json +2 -2
- package/readme.md +5 -0
- package/source/asyncify.d.ts +31 -0
- package/source/entries.d.ts +57 -0
- package/source/entry.d.ts +60 -0
- package/source/iterable-element.d.ts +46 -0
- package/source/literal-union.d.ts +1 -1
- package/source/package-json.d.ts +195 -190
- package/source/promise-value.d.ts +8 -1
- package/source/set-return-type.d.ts +29 -0
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.
|
|
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.
|
|
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;
|
package/source/package-json.d.ts
CHANGED
|
@@ -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
|
-
|
|
342
|
+
Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-json-file). Containing standard npm properties.
|
|
358
343
|
*/
|
|
359
|
-
|
|
344
|
+
export interface PackageJsonStandard {
|
|
345
|
+
/**
|
|
346
|
+
The name of the package.
|
|
347
|
+
*/
|
|
348
|
+
name?: string;
|
|
360
349
|
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
350
|
+
/**
|
|
351
|
+
Package version, parseable by [`node-semver`](https://github.com/npm/node-semver).
|
|
352
|
+
*/
|
|
353
|
+
version?: string;
|
|
365
354
|
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
355
|
+
/**
|
|
356
|
+
Package description, listed in `npm search`.
|
|
357
|
+
*/
|
|
358
|
+
description?: string;
|
|
370
359
|
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
360
|
+
/**
|
|
361
|
+
Keywords associated with package, listed in `npm search`.
|
|
362
|
+
*/
|
|
363
|
+
keywords?: string[];
|
|
375
364
|
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
365
|
+
/**
|
|
366
|
+
The URL to the package's homepage.
|
|
367
|
+
*/
|
|
368
|
+
homepage?: LiteralUnion<'.', string>;
|
|
380
369
|
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
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
|
-
|
|
375
|
+
/**
|
|
376
|
+
The license for the package.
|
|
377
|
+
*/
|
|
378
|
+
license?: string;
|
|
390
379
|
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
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
|
-
|
|
403
|
-
|
|
404
|
-
|
|
390
|
+
/**
|
|
391
|
+
A list of people who contributed to the package.
|
|
392
|
+
*/
|
|
393
|
+
contributors?: Person[];
|
|
405
394
|
|
|
406
|
-
|
|
407
|
-
|
|
395
|
+
/**
|
|
396
|
+
A list of people who maintain the package.
|
|
397
|
+
*/
|
|
398
|
+
maintainers?: Person[];
|
|
408
399
|
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
400
|
+
/**
|
|
401
|
+
The files included in the package.
|
|
402
|
+
*/
|
|
403
|
+
files?: string[];
|
|
412
404
|
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
*/
|
|
416
|
-
main?: string;
|
|
405
|
+
/**
|
|
406
|
+
Resolution algorithm for importing ".js" files from the package's scope.
|
|
417
407
|
|
|
418
|
-
|
|
419
|
-
|
|
408
|
+
[Read more.](https://nodejs.org/api/esm.html#esm_package_json_type_field)
|
|
409
|
+
*/
|
|
410
|
+
type?: 'module' | 'commonjs';
|
|
420
411
|
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
412
|
+
/**
|
|
413
|
+
The module ID that is the primary entry point to the program.
|
|
414
|
+
*/
|
|
415
|
+
main?: string;
|
|
424
416
|
|
|
425
|
-
|
|
426
|
-
|
|
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
|
-
|
|
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
|
-
|
|
441
|
-
|
|
442
|
-
|
|
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
|
-
|
|
446
|
-
|
|
447
|
-
|
|
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
|
-
|
|
439
|
+
Indicates the structure of the package.
|
|
440
|
+
*/
|
|
441
|
+
directories?: DirectoryLocations;
|
|
455
442
|
|
|
456
|
-
|
|
443
|
+
/**
|
|
444
|
+
Location for the code repository.
|
|
457
445
|
*/
|
|
458
|
-
|
|
459
|
-
|
|
446
|
+
repository?:
|
|
447
|
+
| string
|
|
448
|
+
| {
|
|
449
|
+
type: string;
|
|
450
|
+
url: string;
|
|
460
451
|
|
|
461
|
-
|
|
462
|
-
|
|
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
|
-
|
|
468
|
-
|
|
469
|
-
|
|
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
|
-
|
|
475
|
-
|
|
476
|
-
|
|
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
|
-
|
|
480
|
-
|
|
481
|
-
|
|
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
|
-
|
|
485
|
-
|
|
486
|
-
|
|
472
|
+
/**
|
|
473
|
+
The dependencies of the package.
|
|
474
|
+
*/
|
|
475
|
+
dependencies?: Dependency;
|
|
487
476
|
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
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
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
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
|
-
|
|
504
|
-
|
|
505
|
-
|
|
501
|
+
/**
|
|
502
|
+
Package names that are bundled when the package is published.
|
|
503
|
+
*/
|
|
504
|
+
bundledDependencies?: string[];
|
|
506
505
|
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
506
|
+
/**
|
|
507
|
+
Alias of `bundledDependencies`.
|
|
508
|
+
*/
|
|
509
|
+
bundleDependencies?: string[];
|
|
511
510
|
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
511
|
+
/**
|
|
512
|
+
Engines that this package runs on.
|
|
513
|
+
*/
|
|
514
|
+
engines?: {
|
|
515
|
+
[EngineName in 'npm' | 'node' | string]: string;
|
|
516
|
+
};
|
|
518
517
|
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
518
|
+
/**
|
|
519
|
+
@deprecated
|
|
520
|
+
*/
|
|
521
|
+
engineStrict?: boolean;
|
|
523
522
|
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
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
|
-
|
|
547
|
-
|
|
548
|
-
|
|
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
|
-
|
|
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
|
-
|
|
583
|
-
|
|
584
|
-
private?: boolean;
|
|
576
|
+
@deprecated
|
|
577
|
+
*/
|
|
578
|
+
preferGlobal?: boolean;
|
|
585
579
|
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
613
|
-
|
|
614
|
-
|
|
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>
|
|
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
|
+
);
|