type-fest 0.5.1 → 0.7.1

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
@@ -2,13 +2,15 @@
2
2
  export * from './source/basic';
3
3
 
4
4
  // Utilities
5
- export {Omit} from './source/omit';
5
+ export {Except} from './source/except';
6
6
  export {Mutable} from './source/mutable';
7
7
  export {Merge} from './source/merge';
8
8
  export {MergeExclusive} from './source/merge-exclusive';
9
9
  export {RequireAtLeastOne} from './source/require-at-least-one';
10
10
  export {ReadonlyDeep} from './source/readonly-deep';
11
11
  export {LiteralUnion} from './source/literal-union';
12
+ export {Promisable} from './source/promisable';
13
+ export {Opaque} from './source/opaque';
12
14
 
13
15
  // Miscellaneous
14
16
  export {PackageJson} from './source/package-json';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-fest",
3
- "version": "0.5.1",
3
+ "version": "0.7.1",
4
4
  "description": "A collection of essential TypeScript types",
5
5
  "license": "(MIT OR CC0-1.0)",
6
6
  "repository": "sindresorhus/type-fest",
@@ -10,7 +10,7 @@
10
10
  "url": "sindresorhus.com"
11
11
  },
12
12
  "engines": {
13
- "node": ">=6"
13
+ "node": ">=8"
14
14
  },
15
15
  "scripts": {
16
16
  "test": "xo && tsd"
@@ -31,15 +31,13 @@
31
31
  "json"
32
32
  ],
33
33
  "devDependencies": {
34
- "@sindresorhus/tsconfig": "^0.3.0",
35
- "@typescript-eslint/eslint-plugin": "^1.8.0",
36
- "eslint-config-xo-typescript": "^0.11.0",
34
+ "@sindresorhus/tsconfig": "^0.4.0",
35
+ "@typescript-eslint/eslint-plugin": "^1.9.0",
36
+ "@typescript-eslint/parser": "^1.10.2",
37
+ "eslint-config-xo-typescript": "^0.14.0",
37
38
  "tsd": "^0.7.3",
38
39
  "xo": "^0.24.0"
39
40
  },
40
- "peerDependencies": {
41
- "typescript": ">=3.2"
42
- },
43
41
  "xo": {
44
42
  "extends": "xo-typescript",
45
43
  "extensions": [
package/readme.md CHANGED
@@ -30,18 +30,20 @@ PR welcome for additional commonly needed types and docs improvements. Read the
30
30
  $ npm install type-fest
31
31
  ```
32
32
 
33
+ *Requires TypeScript >=3.2*
34
+
33
35
 
34
36
  ## Usage
35
37
 
36
38
  ```ts
37
- import {Omit} from 'type-fest';
39
+ import {Except} from 'type-fest';
38
40
 
39
41
  type Foo = {
40
42
  unicorn: string;
41
43
  rainbow: boolean;
42
44
  };
43
45
 
44
- type FooWithoutRainbow = Omit<Foo, 'rainbow'>;
46
+ type FooWithoutRainbow = Except<Foo, 'rainbow'>;
45
47
  //=> {unicorn: string}
46
48
  ```
47
49
 
@@ -62,13 +64,15 @@ Click the type names for complete docs.
62
64
 
63
65
  ### Utilities
64
66
 
65
- - [`Omit`](source/omit.d.ts) - Create a type from an object type without certain keys.
67
+ - [`Except`](source/except.d.ts) - Create a type from an object type without certain keys. This is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-5.html#the-omit-helper-type).
66
68
  - [`Mutable`](source/mutable.d.ts) - Convert an object with `readonly` properties into a mutable object. Inverse of `Readonly<T>`.
67
69
  - [`Merge`](source/merge.d.ts) - Merge two types into a new type. Keys of the second type overrides keys of the first type.
68
70
  - [`MergeExclusive`](source/merge-exclusive.d.ts) - Create a type that has mutually exclusive properties.
69
71
  - [`RequireAtLeastOne`](source/require-at-least-one.d.ts) - Create a type that requires at least one of the given properties.
70
72
  - [`ReadonlyDeep`](source/readonly-deep.d.ts) - Create a deeply immutable version of a `object`/`Map`/`Set`/`Array` type.
71
73
  - [`LiteralUnion`](source/literal-union.d.ts) - Create a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union. Workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729).
74
+ - [`Promisable`](source/promisable.d.ts) - Create a type that represents either the value or the value wrapped in `PromiseLike`.
75
+ - [`Opaque`](source/opaque.d.ts) - Create an [opaque type](https://codemix.com/opaque-types-in-javascript/).
72
76
 
73
77
  ### Miscellaneous
74
78
 
package/source/basic.d.ts CHANGED
@@ -13,10 +13,11 @@ export type Primitive =
13
13
  | symbol
14
14
  | bigint;
15
15
 
16
+ // TODO: Remove the `= unknown` sometime in the future when most users are on TS 3.5 as it's now the default
16
17
  /**
17
18
  Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
18
19
  */
19
- export type Class<T = unknown> = new(...arguments_: any[]) => T;
20
+ export type Class<T = unknown, Arguments extends any[] = any[]> = new(...arguments_: Arguments) => T;
20
21
 
21
22
  /**
22
23
  Matches any [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), like `Uint8Array` or `Float64Array`.
@@ -36,6 +37,8 @@ export type TypedArray =
36
37
 
37
38
  /**
38
39
  Matches a JSON object.
40
+
41
+ This type can be useful to enforce some input to be JSON-compatible or as a super-type to be extended from. Don't use this as a direct return type as the user would have to double-cast it: `jsonObject as unknown as CustomResponse`. Instead, you could extend your CustomResponse type from it to ensure your type only uses JSON-compatible types: `interface CustomResponse extends JsonObject { … }`.
39
42
  */
40
43
  export type JsonObject = {[key: string]: JsonValue};
41
44
 
@@ -0,0 +1,22 @@
1
+ /**
2
+ Create a type from an object type without certain keys.
3
+
4
+ This type is a stricter version of [`Omit`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-5.html#the-omit-helper-type). The `Omit` type does not restrict the omitted keys to be keys present on the given type, while `Except` does. The benefits of a stricter type are avoiding typos and allowing the compiler to pick up on rename refactors automatically.
5
+
6
+ Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/30825) if you want to have the stricter version as a built-in in TypeScript.
7
+
8
+ @example
9
+ ```
10
+ import {Except} from 'type-fest';
11
+
12
+ type Foo = {
13
+ a: number;
14
+ b: string;
15
+ c: boolean;
16
+ };
17
+
18
+ type FooWithoutA = Except<Foo, 'a' | 'c'>;
19
+ //=> {b: string};
20
+ ```
21
+ */
22
+ export type Except<ObjectType, KeysType extends keyof ObjectType> = Pick<ObjectType, Exclude<keyof ObjectType, KeysType>>;
package/source/merge.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import {Omit} from './omit';
1
+ import {Except} from './except';
2
2
 
3
3
  /**
4
4
  Merge two types into a new type. Keys of the second type overrides keys of the first type.
@@ -19,4 +19,4 @@ type Bar = {
19
19
  const ab: Merge<Foo, Bar> = {a: 1, b: 2};
20
20
  ```
21
21
  */
22
- export type Merge<FirstType, SecondType> = Omit<FirstType, Extract<keyof FirstType, keyof SecondType>> & SecondType;
22
+ export type Merge<FirstType, SecondType> = Except<FirstType, Extract<keyof FirstType, keyof SecondType>> & SecondType;
@@ -0,0 +1,40 @@
1
+ /**
2
+ Create an opaque type, which hides its internal details from the public, and can only be created by being used explicitly.
3
+
4
+ The generic type parameter can be anything. It doesn't have to be an object.
5
+
6
+ [Read more about opaque types.](https://codemix.com/opaque-types-in-javascript/)
7
+
8
+ There have been several discussions about adding this feature to TypeScript via the `opaque type` operator, similar to how Flow does it. Unfortunately, nothing has (yet) moved forward:
9
+ - [Microsoft/TypeScript#15408](https://github.com/Microsoft/TypeScript/issues/15408)
10
+ - [Microsoft/TypeScript#15807](https://github.com/Microsoft/TypeScript/issues/15807)
11
+
12
+ @example
13
+ ```
14
+ import {Opaque} from 'type-fest';
15
+
16
+ type AccountNumber = Opaque<number>;
17
+ type AccountBalance = Opaque<number>;
18
+
19
+ function createAccountNumber(): AccountNumber {
20
+ return 2 as AccountNumber;
21
+ }
22
+
23
+ function getMoneyForAccount(accountNumber: AccountNumber): AccountBalance {
24
+ return 4 as AccountBalance;
25
+ }
26
+
27
+ // This will compile successfully.
28
+ getMoneyForAccount(createAccountNumber());
29
+
30
+ // But this won't, because it has to be explicitly passed as an `AccountNumber` type.
31
+ getMoneyForAccount(2);
32
+
33
+ // You can use opaque values like they aren't opaque too.
34
+ const accountNumber = createAccountNumber();
35
+
36
+ // This will compile successfully.
37
+ accountNumber + 2;
38
+ ```
39
+ */
40
+ export type Opaque<Type> = Type & {readonly __opaque__: unique symbol};
@@ -251,6 +251,13 @@ declare namespace PackageJson {
251
251
  }
252
252
 
253
253
  export interface YarnConfiguration {
254
+ /**
255
+ If your package only allows one version of a given dependency, and you’d like to enforce the same behavior as `yarn install --flat` on the command line, set this to `true`.
256
+
257
+ Note that if your `package.json` contains `"flat": true` and other packages depend on yours (e.g. you are building a library rather than an application), those other packages will also need `"flat": true` in their `package.json` or be installed with `yarn install --flat` on the command-line.
258
+ */
259
+ flat?: boolean;
260
+
254
261
  /**
255
262
  Selective version resolutions. Allows the definition of custom package versions inside dependencies without manual edits in the `yarn.lock` file.
256
263
  */
@@ -0,0 +1,23 @@
1
+ /**
2
+ Create a type that represents either the value or the value wrapped in `PromiseLike`.
3
+
4
+ Use-cases:
5
+ - A function accepts a callback that may either return a value synchronously or may return a promised value.
6
+ - This type could be the return type of `Promise#then()`, `Promise#catch()`, and `Promise#finally()` callbacks.
7
+
8
+ Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/31394) if you want to have this type as a built-in in TypeScript.
9
+
10
+ @example
11
+ ```
12
+ import {Promisable} from 'type-fest';
13
+
14
+ async function logger(getLogEntry: () => Promisable<string>): Promise<void> {
15
+ const entry = await getLogEntry();
16
+ console.log(entry);
17
+ }
18
+
19
+ logger(() => 'foo');
20
+ logger(() => Promise.resolve('bar'));
21
+ ```
22
+ */
23
+ export type Promisable<T> = T | PromiseLike<T>;
@@ -1,4 +1,4 @@
1
- import {Omit} from './omit';
1
+ import {Except} from './except';
2
2
 
3
3
  /**
4
4
  Create a type that requires at least one of the given properties. The remaining properties are kept as is.
@@ -29,4 +29,4 @@ export type RequireAtLeastOne<ObjectType, KeysType extends keyof ObjectType = ke
29
29
  )
30
30
  }[KeysType]
31
31
  // …then, make intersection types by adding the remaining properties to each mapped type.
32
- & Omit<ObjectType, KeysType>;
32
+ & Except<ObjectType, KeysType>;
package/source/omit.d.ts DELETED
@@ -1,17 +0,0 @@
1
- /**
2
- Create a type from an object type without certain keys.
3
-
4
- @example
5
- ```
6
- import {Omit} from 'type-fest';
7
-
8
- type Foo = {
9
- a: number;
10
- b: string;
11
- };
12
-
13
- type FooWithoutA = Omit<Foo, 'a'>;
14
- //=> {b: string};
15
- ```
16
- */
17
- export type Omit<ObjectType, KeysType extends keyof ObjectType> = Pick<ObjectType, Exclude<keyof ObjectType, KeysType>>;