type-fest 0.2.0 → 0.4.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
@@ -1,130 +1,13 @@
1
+ // Basic
2
+ export * from './source/basic';
3
+
4
+ // Utilities
5
+ export {Omit} from './source/omit';
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 {LiteralUnion} from './source/literal-union';
11
+
12
+ // Miscellaneous
1
13
  export {PackageJson} from './source/package-json';
2
-
3
- // TODO: Add more examples
4
-
5
- // TODO: This can just be `export type Primitive = not object` when the `not` keyword is out.
6
- /**
7
- Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
8
- */
9
- export type Primitive =
10
- | null
11
- | undefined
12
- | string
13
- | number
14
- | boolean
15
- | symbol;
16
-
17
- /**
18
- Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
19
- */
20
- export type Class<T = unknown> = new(...arguments_: any[]) => T;
21
-
22
- /**
23
- Matches any [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), like `Uint8Array` or `Float64Array`.
24
- */
25
- export type TypedArray =
26
- | Int8Array
27
- | Uint8Array
28
- | Uint8ClampedArray
29
- | Int16Array
30
- | Uint16Array
31
- | Int32Array
32
- | Uint32Array
33
- | Float32Array
34
- | Float64Array;
35
-
36
- /**
37
- Matches a JSON object.
38
- */
39
- export type JsonObject = {[key: string]: JsonValue};
40
-
41
- /**
42
- Matches a JSON array.
43
- */
44
- export interface JsonArray extends Array<JsonValue> {} // eslint-disable-line @typescript-eslint/no-empty-interface
45
-
46
- /**
47
- Matches any valid JSON value.
48
- */
49
- export type JsonValue = string | number | boolean | null | JsonObject | JsonArray;
50
-
51
- /**
52
- Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable).
53
- */
54
- export interface ObservableLike {
55
- subscribe(observer: (value: unknown) => void): void;
56
- [Symbol.observable](): ObservableLike;
57
- }
58
-
59
- /**
60
- Create a type from an object type without certain keys.
61
-
62
- @example
63
- ```
64
- import {Omit} from 'type-fest';
65
-
66
- type Foo = {
67
- a: number;
68
- b: string;
69
- };
70
-
71
- type FooWithoutA = Omit<Foo, 'a'>;
72
- //=> {b: string};
73
- ```
74
-
75
- I'm surprised this one is not built-in. It seems [other people agree](https://github.com/Microsoft/TypeScript/issues/12215#issuecomment-420919470). Please open new issues on TypeScript about making it built-in.
76
- */
77
- export type Omit<ObjectType, KeysType extends keyof ObjectType> = Pick<ObjectType, Exclude<keyof ObjectType, KeysType>>;
78
-
79
- /**
80
- Merge two types into a new type. Keys of the second type overrides keys of the first type.
81
-
82
- @example
83
- ```
84
- import {Merge} from 'type-fest';
85
-
86
- type Foo = {
87
- a: number;
88
- b: string;
89
- };
90
-
91
- type Bar = {
92
- b: number;
93
- };
94
-
95
- const ab: Merge<Foo, Bar> = {a: 1, b: 2};
96
- ```
97
- */
98
- export type Merge<FirstType, SecondType> = Omit<FirstType, Extract<keyof FirstType, keyof SecondType>> & SecondType;
99
-
100
- /**
101
- Allows creating a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union.
102
-
103
- Currently, when a union type of a primitive type is combined with literal types, TypeScript loses all information about the combined literals. Thus, when such type is used in an IDE with autocompletion, no suggestions are made for the declared literals.
104
-
105
- This type is a workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729). It will be removed as soon as it's not needed anymore.
106
-
107
- @example
108
- ```
109
- import {LiteralUnion} from 'type-fest';
110
-
111
- // Before
112
-
113
- type Pet = 'dog' | 'cat' | string;
114
-
115
- const pet: Pet = '';
116
- // Start typing in your TypeScript-enabled IDE.
117
- // You **will not** get auto-completion for `dog` and `cat` literals.
118
-
119
- // After
120
-
121
- type Pet2 = LiteralUnion<'dog' | 'cat', string>;
122
-
123
- const pet: Pet2 = '';
124
- // You **will** get auto-completion for `dog` and `cat` literals.
125
- ```
126
- */
127
- export type LiteralUnion<
128
- LiteralType extends BaseType,
129
- BaseType extends Primitive
130
- > = LiteralType | (BaseType & {_?: never});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-fest",
3
- "version": "0.2.0",
3
+ "version": "0.4.1",
4
4
  "description": "A collection of essential TypeScript types",
5
5
  "license": "(MIT OR CC0-1.0)",
6
6
  "repository": "sindresorhus/type-fest",
@@ -13,7 +13,7 @@
13
13
  "node": ">=6"
14
14
  },
15
15
  "scripts": {
16
- "test": "xo && tsd-check"
16
+ "test": "xo && tsd"
17
17
  },
18
18
  "files": [
19
19
  "index.d.ts",
@@ -31,9 +31,10 @@
31
31
  "json"
32
32
  ],
33
33
  "devDependencies": {
34
- "@typescript-eslint/eslint-plugin": "^1.4.2",
35
- "eslint-config-xo-typescript": "^0.8.0",
36
- "tsd-check": "^0.3.0",
34
+ "@sindresorhus/tsconfig": "^0.3.0",
35
+ "@typescript-eslint/eslint-plugin": "^1.5.0",
36
+ "eslint-config-xo-typescript": "^0.10.0",
37
+ "tsd": "^0.7.2",
37
38
  "xo": "^0.24.0"
38
39
  },
39
40
  "xo": {
@@ -42,7 +43,8 @@
42
43
  "ts"
43
44
  ],
44
45
  "rules": {
45
- "import/no-unresolved": "off"
46
+ "import/no-unresolved": "off",
47
+ "@typescript-eslint/indent": "off"
46
48
  }
47
49
  }
48
50
  }
package/readme.md CHANGED
@@ -48,27 +48,30 @@ type FooWithoutRainbow = Omit<Foo, 'rainbow'>;
48
48
 
49
49
  ## API
50
50
 
51
- See the [types file](index.d.ts) for complete docs.
51
+ Click the type names for complete docs.
52
52
 
53
53
  ### Basic
54
54
 
55
- - `Primitive` - Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
56
- - `Class` - Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
57
- - `TypedArray` - Matches any [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), like `Uint8Array` or `Float64Array`.
58
- - `JsonObject` - Matches a JSON object.
59
- - `JsonArray` - Matches a JSON array.
60
- - `JsonValue` - Matches any valid JSON value.
61
- - `ObservableLike` - Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable).
55
+ - [`Primitive`](source/basic.d.ts) - Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
56
+ - [`Class`](source/basic.d.ts) - Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
57
+ - [`TypedArray`](source/basic.d.ts) - Matches any [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), like `Uint8Array` or `Float64Array`.
58
+ - [`JsonObject`](source/basic.d.ts) - Matches a JSON object.
59
+ - [`JsonArray`](source/basic.d.ts) - Matches a JSON array.
60
+ - [`JsonValue`](source/basic.d.ts) - Matches any valid JSON value.
61
+ - [`ObservableLike`](source/basic.d.ts) - Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable).
62
62
 
63
63
  ### Utilities
64
64
 
65
- - `Omit` - Create a type from an object type without certain keys.
66
- - `Merge` - Merge two types into a new type. Keys of the second type overrides keys of the first type.
67
- - `LiteralUnion` - Allows creating 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).
65
+ - [`Omit`](source/omit.d.ts) - Create a type from an object type without certain keys.
66
+ - [`Mutable`](source/mutable.d.ts) - Convert an object with `readonly` properties into a mutable object. Inverse of `Readonly<T>`.
67
+ - [`Merge`](source/merge.d.ts) - Merge two types into a new type. Keys of the second type overrides keys of the first type.
68
+ - [`MergeExclusive`](source/merge-exclusive.d.ts) - Create a type that has mutually exclusive properties.
69
+ - [`RequireAtLeastOne`](source/require-at-least-one.d.ts) - Create a type that requires at least one of the given properties.
70
+ - [`LiteralUnion`](source/literal-union.d.ts) - Allows creating 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).
68
71
 
69
72
  ### Miscellaneous
70
73
 
71
- - `PackageJson` - Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-json-file).
74
+ - [`PackageJson`](source/package-json.d.ts) - Type for [npm's `package.json` file](https://docs.npmjs.com/creating-a-package-json-file).
72
75
 
73
76
 
74
77
  ## Declined types
@@ -98,6 +101,13 @@ There are many advanced types most users don't know about.
98
101
  You can find some examples in the [TypeScript docs](https://www.typescriptlang.org/docs/handbook/advanced-types.html#predefined-conditional-types).
99
102
 
100
103
 
104
+ ## Maintainers
105
+
106
+ - [Sindre Sorhus](https://github.com/sindresorhus)
107
+ - [Jarek Radosz](https://github.com/CvX)
108
+ - [Dimitri Benin](https://github.com/BendingBender)
109
+
110
+
101
111
  ## License
102
112
 
103
113
  (MIT OR CC0-1.0)
@@ -0,0 +1,59 @@
1
+ // TODO: This can just be `export type Primitive = not object` when the `not` keyword is out.
2
+ /**
3
+ Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
4
+ */
5
+ export type Primitive =
6
+ | null
7
+ | undefined
8
+ | string
9
+ | number
10
+ | boolean
11
+ | symbol;
12
+
13
+ /**
14
+ Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
15
+ */
16
+ export type Class<T = unknown> = new(...arguments_: any[]) => T;
17
+
18
+ /**
19
+ Matches any [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), like `Uint8Array` or `Float64Array`.
20
+ */
21
+ export type TypedArray =
22
+ | Int8Array
23
+ | Uint8Array
24
+ | Uint8ClampedArray
25
+ | Int16Array
26
+ | Uint16Array
27
+ | Int32Array
28
+ | Uint32Array
29
+ | Float32Array
30
+ | Float64Array;
31
+
32
+ /**
33
+ Matches a JSON object.
34
+ */
35
+ export type JsonObject = {[key: string]: JsonValue};
36
+
37
+ /**
38
+ Matches a JSON array.
39
+ */
40
+ export interface JsonArray extends Array<JsonValue> {}
41
+
42
+ /**
43
+ Matches any valid JSON value.
44
+ */
45
+ export type JsonValue = string | number | boolean | null | JsonObject | JsonArray;
46
+
47
+ declare global {
48
+ interface SymbolConstructor {
49
+ readonly observable: symbol;
50
+ }
51
+ }
52
+
53
+ /**
54
+ Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable).
55
+ */
56
+ export interface ObservableLike {
57
+ subscribe(observer: (value: unknown) => void): void;
58
+ [Symbol.observable](): ObservableLike;
59
+ }
@@ -0,0 +1,33 @@
1
+ import {Primitive} from './basic';
2
+
3
+ /**
4
+ Allows creating a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union.
5
+
6
+ Currently, when a union type of a primitive type is combined with literal types, TypeScript loses all information about the combined literals. Thus, when such type is used in an IDE with autocompletion, no suggestions are made for the declared literals.
7
+
8
+ This type is a workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729). It will be removed as soon as it's not needed anymore.
9
+
10
+ @example
11
+ ```
12
+ import {LiteralUnion} from 'type-fest';
13
+
14
+ // Before
15
+
16
+ type Pet = 'dog' | 'cat' | string;
17
+
18
+ const pet: Pet = '';
19
+ // Start typing in your TypeScript-enabled IDE.
20
+ // You **will not** get auto-completion for `dog` and `cat` literals.
21
+
22
+ // After
23
+
24
+ type Pet2 = LiteralUnion<'dog' | 'cat', string>;
25
+
26
+ const pet: Pet2 = '';
27
+ // You **will** get auto-completion for `dog` and `cat` literals.
28
+ ```
29
+ */
30
+ export type LiteralUnion<
31
+ LiteralType extends BaseType,
32
+ BaseType extends Primitive
33
+ > = LiteralType | (BaseType & {_?: never});
@@ -0,0 +1,39 @@
1
+ // Helper type. Not useful on its own.
2
+ type Without<FirstType, SecondType> = {[KeyType in Exclude<keyof FirstType, keyof SecondType>]?: never};
3
+
4
+ /**
5
+ Create a type that has mutually exclusive properties.
6
+
7
+ This type was inspired by [this comment](https://github.com/Microsoft/TypeScript/issues/14094#issuecomment-373782604).
8
+
9
+ This type works with a helper type, called `Without`. `Without<FirstType, SecondType>` produces a type that has only keys from `FirstType` which are not present on `SecondType` and sets the value type for these keys to `never`. This helper type is then used in `MergeExclusive` to remove keys from either `FirstType` or `SecondType`.
10
+
11
+ @example
12
+ ```
13
+ import {MergeExclusive} from 'type-fest';
14
+
15
+ interface ExclusiveVariation1 {
16
+ exclusive1: boolean;
17
+ }
18
+
19
+ interface ExclusiveVariation2 {
20
+ exclusive2: string;
21
+ }
22
+
23
+ type ExclusiveOptions = MergeExclusive<ExclusiveVariation1, ExclusiveVariation2>;
24
+
25
+ let exclusiveOptions: ExclusiveOptions;
26
+
27
+ exclusiveOptions = {exclusive1: true};
28
+ //=> Works
29
+ exclusiveOptions = {exclusive2: 'hi'};
30
+ //=> Works
31
+ exclusiveOptions = {exclusive1: true, exclusive2: 'hi'};
32
+ //=> Error
33
+ ```
34
+ */
35
+ export type MergeExclusive<FirstType, SecondType> =
36
+ (FirstType | SecondType) extends object ?
37
+ (Without<FirstType, SecondType> & SecondType) | (Without<SecondType, FirstType> & FirstType) :
38
+ FirstType | SecondType;
39
+
@@ -0,0 +1,22 @@
1
+ import {Omit} from './omit';
2
+
3
+ /**
4
+ Merge two types into a new type. Keys of the second type overrides keys of the first type.
5
+
6
+ @example
7
+ ```
8
+ import {Merge} from 'type-fest';
9
+
10
+ type Foo = {
11
+ a: number;
12
+ b: string;
13
+ };
14
+
15
+ type Bar = {
16
+ b: number;
17
+ };
18
+
19
+ const ab: Merge<Foo, Bar> = {a: 1, b: 2};
20
+ ```
21
+ */
22
+ export type Merge<FirstType, SecondType> = Omit<FirstType, Extract<keyof FirstType, keyof SecondType>> & SecondType;
@@ -0,0 +1,22 @@
1
+ /**
2
+ Convert an object with `readonly` properties into a mutable object. Inverse of `Readonly<T>`.
3
+
4
+ This can be used to [store and mutate options within a class](https://github.com/sindresorhus/pageres/blob/4a5d05fca19a5fbd2f53842cbf3eb7b1b63bddd2/source/index.ts#L72), [edit `readonly` objects within tests](https://stackoverflow.com/questions/50703834), and [construct a `readonly` object within a function](https://github.com/Microsoft/TypeScript/issues/24509).
5
+
6
+ @example
7
+ ```
8
+ import {Mutable} from 'type-fest';
9
+
10
+ type Foo = {
11
+ readonly a: number;
12
+ readonly b: string;
13
+ };
14
+
15
+ const mutableFoo: Mutable<Foo> = {a: 1, b: '2'};
16
+ mutableFoo.a = 3;
17
+ ```
18
+ */
19
+ export type Mutable<ObjectType> = {
20
+ // For each `Key` in the keys of `ObjectType`, make a mapped type by removing the `readonly` modifier from the property.
21
+ -readonly [KeyType in keyof ObjectType]: ObjectType[KeyType];
22
+ }
@@ -0,0 +1,17 @@
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>>;
@@ -307,10 +307,10 @@ export type PackageJson = {
307
307
  /**
308
308
  The licenses for the package.
309
309
  */
310
- licenses?: {
310
+ licenses?: Array<{
311
311
  type?: string;
312
312
  url?: string;
313
- }[];
313
+ }>;
314
314
 
315
315
  author?: PackageJson.Person;
316
316
 
@@ -420,7 +420,7 @@ export type PackageJson = {
420
420
  /**
421
421
  Operating systems the module runs on.
422
422
  */
423
- os?: LiteralUnion<
423
+ os?: Array<LiteralUnion<
424
424
  | 'aix'
425
425
  | 'darwin'
426
426
  | 'freebsd'
@@ -436,12 +436,12 @@ export type PackageJson = {
436
436
  | '!sunos'
437
437
  | '!win32',
438
438
  string
439
- >[];
439
+ >>;
440
440
 
441
441
  /**
442
442
  CPU architectures the module runs on.
443
443
  */
444
- cpu?: LiteralUnion<
444
+ cpu?: Array<LiteralUnion<
445
445
  | 'arm'
446
446
  | 'arm64'
447
447
  | 'ia32'
@@ -465,7 +465,7 @@ export type PackageJson = {
465
465
  | '!x32'
466
466
  | '!x64',
467
467
  string
468
- >[];
468
+ >>;
469
469
 
470
470
  /**
471
471
  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.
@@ -0,0 +1,32 @@
1
+ import {Omit} from './omit';
2
+
3
+ /**
4
+ Create a type that requires at least one of the given properties. The remaining properties are kept as is.
5
+
6
+ @example
7
+ ```
8
+ import {RequireAtLeastOne} from 'type-fest';
9
+
10
+ type Responder = {
11
+ text?: () => string;
12
+ json?: () => string;
13
+
14
+ secure?: boolean;
15
+ };
16
+
17
+ const responder: RequireAtLeastOne<Responder, 'text' | 'json'> = {
18
+ json: () => '{"message": "ok"}',
19
+ secure: true
20
+ };
21
+ ```
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 properties to each mapped type.
32
+ & Omit<ObjectType, KeysType>;