type-fest 0.13.1 → 0.16.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
@@ -23,6 +23,7 @@ export {ConditionalKeys} from './source/conditional-keys';
23
23
  export {ConditionalPick} from './source/conditional-pick';
24
24
  export {UnionToIntersection} from './source/union-to-intersection';
25
25
  export {Stringified} from './source/stringified';
26
+ export {FixedLengthArray} from './source/fixed-length-array';
26
27
 
27
28
  // Miscellaneous
28
29
  export {PackageJson} from './source/package-json';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-fest",
3
- "version": "0.13.1",
3
+ "version": "0.16.0",
4
4
  "description": "A collection of essential TypeScript types",
5
5
  "license": "(MIT OR CC0-1.0)",
6
6
  "repository": "sindresorhus/type-fest",
package/readme.md CHANGED
@@ -12,7 +12,7 @@
12
12
  <br>
13
13
 
14
14
  [![Build Status](https://travis-ci.com/sindresorhus/type-fest.svg?branch=master)](https://travis-ci.com/sindresorhus/type-fest)
15
- [![](https://img.shields.io/badge/unicorn-approved-ff69b4.svg)](https://www.youtube.com/watch?v=9auOCbH5Ns4)
15
+ [![](https://img.shields.io/badge/unicorn-approved-ff69b4.svg)](https://giphy.com/gifs/illustration-rainbow-unicorn-26AHG5KGFxSkUWw1i)
16
16
  <!-- Commented out until they actually show anything
17
17
  [![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)
18
18
  -->
@@ -82,6 +82,7 @@ Click the type names for complete docs.
82
82
  - [`ConditionalExcept`](source/conditional-except.d.ts) - Like `Omit` except it removes properties from a shape where the values extend the given `Condition` type.
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
+ - [`FixedLengthArray`](source/fixed-length-array.d.ts) - Create a type that represents an array of the given type and length.
85
86
 
86
87
  ### Miscellaneous
87
88
 
@@ -0,0 +1,38 @@
1
+ /**
2
+ Methods to exclude.
3
+ */
4
+ type ArrayLengthMutationKeys = 'splice' | 'push' | 'pop' | 'shift' | 'unshift';
5
+
6
+ /**
7
+ Create a type that represents an array of the given type and length. The array's length and the `Array` prototype methods that manipulate its length are excluded in the resulting type.
8
+
9
+ Please participate in [this issue](https://github.com/microsoft/TypeScript/issues/26223) if you want to have a similiar type built into TypeScript.
10
+
11
+ Use-cases:
12
+ - Declaring fixed-length tuples or arrays with a large number of items.
13
+ - Creating a range union (for example, `0 | 1 | 2 | 3 | 4` from the keys of such a type) without having to resort to recursive types.
14
+ - Creating an array of coordinates with a static length, for example, length of 3 for a 3D vector.
15
+
16
+ @example
17
+ ```
18
+ import {FixedLengthArray} from 'type-fest';
19
+
20
+ type FencingTeam = FixedLengthArray<string, 3>;
21
+
22
+ const guestFencingTeam: FencingTeam = ['Josh', 'Michael', 'Robert'];
23
+
24
+ const homeFencingTeam: FencingTeam = ['George', 'John'];
25
+ //=> error TS2322: Type string[] is not assignable to type 'FencingTeam'
26
+
27
+ guestFencingTeam.push('Sam');
28
+ //=> error TS2339: Property 'push' does not exist on type 'FencingTeam'
29
+ ```
30
+ */
31
+ export type FixedLengthArray<Element, Length extends number, ArrayPrototype = [Element, ...Element[]]> = Pick<
32
+ ArrayPrototype,
33
+ Exclude<keyof ArrayPrototype, ArrayLengthMutationKeys>
34
+ > & {
35
+ [index: number]: Element;
36
+ [Symbol.iterator]: () => IterableIterator<Element>;
37
+ readonly length: Length;
38
+ };
@@ -1,4 +1,4 @@
1
- import {LiteralUnion} from '..';
1
+ import {LiteralUnion} from './literal-union';
2
2
 
3
3
  declare namespace PackageJson {
4
4
  /**
@@ -211,6 +211,29 @@ declare namespace PackageJson {
211
211
  [packageName: string]: string;
212
212
  }
213
213
 
214
+ /**
215
+ Conditions which provide a way to resolve a package entry point based on the environment.
216
+ */
217
+ export type ExportCondition = LiteralUnion<
218
+ | 'import'
219
+ | 'require'
220
+ | 'node'
221
+ | 'deno'
222
+ | 'browser'
223
+ | 'electron'
224
+ | 'react-native'
225
+ | 'default',
226
+ string
227
+ >;
228
+
229
+ /**
230
+ Entry points of a module, optionally with conditions and subpath exports.
231
+ */
232
+ export type Exports =
233
+ | string
234
+ | {[key in ExportCondition]: Exports}
235
+ | {[key: string]: Exports};
236
+
214
237
  export interface NonStandardEntryPoints {
215
238
  /**
216
239
  An ECMAScript module ID that is the primary entry point to the program.
@@ -380,11 +403,25 @@ export type PackageJson = {
380
403
  */
381
404
  files?: string[];
382
405
 
406
+ /**
407
+ Resolution algorithm for importing ".js" files from the package's scope.
408
+
409
+ [Read more.](https://nodejs.org/api/esm.html#esm_package_json_type_field)
410
+ */
411
+ type?: 'module' | 'commonjs';
412
+
383
413
  /**
384
414
  The module ID that is the primary entry point to the program.
385
415
  */
386
416
  main?: string;
387
417
 
418
+ /**
419
+ Standard entry points of the package, with enhanced support for ECMAScript Modules.
420
+
421
+ [Read more.](https://nodejs.org/api/esm.html#esm_package_entry_points)
422
+ */
423
+ exports?: PackageJson.Exports;
424
+
388
425
  /**
389
426
  The executable files that should be installed into the `PATH`.
390
427
  */
@@ -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;
@@ -42,7 +42,7 @@ type Intersection = UnionToIntersection<Union>;
42
42
  ```
43
43
  */
44
44
  export type UnionToIntersection<Union> = (
45
- // `extends any` is always going to be the case and is used to convert the
45
+ // `extends unknown` is always going to be the case and is used to convert the
46
46
  // `Union` into a [distributive conditional
47
47
  // type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
48
48
  Union extends unknown