type-fest 0.4.1 → 0.6.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 +3 -1
- package/package.json +7 -6
- package/readme.md +10 -4
- package/source/basic.d.ts +10 -2
- package/source/except.d.ts +22 -0
- package/source/merge.d.ts +2 -2
- package/source/mutable.d.ts +1 -1
- package/source/package-json.d.ts +8 -1
- package/source/promisable.d.ts +23 -0
- package/source/readonly-deep.d.ts +59 -0
- package/source/require-at-least-one.d.ts +2 -2
- package/source/omit.d.ts +0 -17
package/index.d.ts
CHANGED
|
@@ -2,12 +2,14 @@
|
|
|
2
2
|
export * from './source/basic';
|
|
3
3
|
|
|
4
4
|
// Utilities
|
|
5
|
-
export {
|
|
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
|
+
export {ReadonlyDeep} from './source/readonly-deep';
|
|
10
11
|
export {LiteralUnion} from './source/literal-union';
|
|
12
|
+
export {Promisable} from './source/promisable';
|
|
11
13
|
|
|
12
14
|
// Miscellaneous
|
|
13
15
|
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.6.0",
|
|
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": ">=
|
|
13
|
+
"node": ">=8"
|
|
14
14
|
},
|
|
15
15
|
"scripts": {
|
|
16
16
|
"test": "xo && tsd"
|
|
@@ -31,10 +31,11 @@
|
|
|
31
31
|
"json"
|
|
32
32
|
],
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@sindresorhus/tsconfig": "^0.
|
|
35
|
-
"@typescript-eslint/eslint-plugin": "^1.
|
|
36
|
-
"eslint
|
|
37
|
-
"
|
|
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",
|
|
38
|
+
"tsd": "^0.7.3",
|
|
38
39
|
"xo": "^0.24.0"
|
|
39
40
|
},
|
|
40
41
|
"xo": {
|
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 {
|
|
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 =
|
|
46
|
+
type FooWithoutRainbow = Except<Foo, 'rainbow'>;
|
|
45
47
|
//=> {unicorn: string}
|
|
46
48
|
```
|
|
47
49
|
|
|
@@ -62,12 +64,14 @@ Click the type names for complete docs.
|
|
|
62
64
|
|
|
63
65
|
### Utilities
|
|
64
66
|
|
|
65
|
-
- [`
|
|
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.
|
|
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`.
|
|
71
75
|
|
|
72
76
|
### Miscellaneous
|
|
73
77
|
|
|
@@ -78,6 +82,8 @@ Click the type names for complete docs.
|
|
|
78
82
|
|
|
79
83
|
*If we decline a type addition, we will make sure to document the better solution here.*
|
|
80
84
|
|
|
85
|
+
- [`Diff` and `Spread`](https://github.com/sindresorhus/type-fest/pull/7) - The PR author didn't provide any real-world use-cases and the PR went stale. If you think this type is useful, provide some real-world use-cases and we might reconsider.
|
|
86
|
+
|
|
81
87
|
|
|
82
88
|
## Tips
|
|
83
89
|
|
package/source/basic.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/// <reference lib="esnext"/>
|
|
2
|
+
|
|
1
3
|
// TODO: This can just be `export type Primitive = not object` when the `not` keyword is out.
|
|
2
4
|
/**
|
|
3
5
|
Matches any [primitive value](https://developer.mozilla.org/en-US/docs/Glossary/Primitive).
|
|
@@ -8,8 +10,10 @@ export type Primitive =
|
|
|
8
10
|
| string
|
|
9
11
|
| number
|
|
10
12
|
| boolean
|
|
11
|
-
| symbol
|
|
13
|
+
| symbol
|
|
14
|
+
| bigint;
|
|
12
15
|
|
|
16
|
+
// TODO: Remove the `= unknown` sometime in the future when most users are on TS 3.5 as it's now the default
|
|
13
17
|
/**
|
|
14
18
|
Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
|
|
15
19
|
*/
|
|
@@ -27,10 +31,14 @@ export type TypedArray =
|
|
|
27
31
|
| Int32Array
|
|
28
32
|
| Uint32Array
|
|
29
33
|
| Float32Array
|
|
30
|
-
| Float64Array
|
|
34
|
+
| Float64Array
|
|
35
|
+
| BigInt64Array
|
|
36
|
+
| BigUint64Array;
|
|
31
37
|
|
|
32
38
|
/**
|
|
33
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 { … }`.
|
|
34
42
|
*/
|
|
35
43
|
export type JsonObject = {[key: string]: JsonValue};
|
|
36
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 {
|
|
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> =
|
|
22
|
+
export type Merge<FirstType, SecondType> = Except<FirstType, Extract<keyof FirstType, keyof SecondType>> & SecondType;
|
package/source/mutable.d.ts
CHANGED
package/source/package-json.d.ts
CHANGED
|
@@ -202,7 +202,7 @@ declare namespace PackageJson {
|
|
|
202
202
|
postrestart?: string;
|
|
203
203
|
} & {
|
|
204
204
|
[scriptName: string]: string;
|
|
205
|
-
}
|
|
205
|
+
};
|
|
206
206
|
|
|
207
207
|
/**
|
|
208
208
|
Dependencies of the package. The version range is a string which has one or more space-separated descriptors. Dependencies can also be identified with a tarball or Git URL.
|
|
@@ -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>;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {Primitive} from './basic';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Convert `object`s, `Map`s, `Set`s, and `Array`s and all of their properties/elements into immutable structures recursively.
|
|
5
|
+
|
|
6
|
+
This is useful when a deeply nested structure needs to be exposed as completely immutable, for example, an imported JSON module or when receiving an API response that is passed around.
|
|
7
|
+
|
|
8
|
+
Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/13923) if you want to have this type as a built-in in TypeScript.
|
|
9
|
+
|
|
10
|
+
@example
|
|
11
|
+
```
|
|
12
|
+
// data.json
|
|
13
|
+
{
|
|
14
|
+
"foo": ["bar"]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// main.ts
|
|
18
|
+
import {ReadonlyDeep} from 'type-fest';
|
|
19
|
+
import dataJson = require('./data.json');
|
|
20
|
+
|
|
21
|
+
const data: ReadonlyDeep<typeof dataJson> = dataJson;
|
|
22
|
+
|
|
23
|
+
export default data;
|
|
24
|
+
|
|
25
|
+
// test.ts
|
|
26
|
+
import data from './main';
|
|
27
|
+
|
|
28
|
+
data.foo.push('bar');
|
|
29
|
+
//=> error TS2339: Property 'push' does not exist on type 'readonly string[]'
|
|
30
|
+
```
|
|
31
|
+
*/
|
|
32
|
+
export type ReadonlyDeep<T> = T extends Primitive | ((...arguments: any[]) => unknown)
|
|
33
|
+
? T
|
|
34
|
+
: T extends ReadonlyMap<infer KeyType, infer ValueType>
|
|
35
|
+
? ReadonlyMapDeep<KeyType, ValueType>
|
|
36
|
+
: T extends ReadonlySet<infer ItemType>
|
|
37
|
+
? ReadonlySetDeep<ItemType>
|
|
38
|
+
: T extends object
|
|
39
|
+
? ReadonlyObjectDeep<T>
|
|
40
|
+
: unknown;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
Same as `ReadonlyDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `ReadonlyDeep`.
|
|
44
|
+
*/
|
|
45
|
+
interface ReadonlyMapDeep<KeyType, ValueType>
|
|
46
|
+
extends ReadonlyMap<ReadonlyDeep<KeyType>, ReadonlyDeep<ValueType>> {}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
Same as `ReadonlyDeep`, but accepts only `ReadonlySet`s as inputs. Internal helper for `ReadonlyDeep`.
|
|
50
|
+
*/
|
|
51
|
+
interface ReadonlySetDeep<ItemType>
|
|
52
|
+
extends ReadonlySet<ReadonlyDeep<ItemType>> {}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
Same as `ReadonlyDeep`, but accepts only `object`s as inputs. Internal helper for `ReadonlyDeep`.
|
|
56
|
+
*/
|
|
57
|
+
type ReadonlyObjectDeep<ObjectType extends object> = {
|
|
58
|
+
readonly [PropertyType in keyof ObjectType]: ReadonlyDeep<ObjectType[PropertyType]>
|
|
59
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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
|
-
&
|
|
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>>;
|