type-fest 2.12.2 → 2.13.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 +4 -0
- package/package.json +4 -4
- package/readme.md +73 -2
- package/source/async-return-type.d.ts +2 -2
- package/source/asyncify.d.ts +3 -3
- package/source/camel-case.d.ts +3 -3
- package/source/camel-cased-properties-deep.d.ts +3 -1
- package/source/camel-cased-properties.d.ts +3 -1
- package/source/conditional-except.d.ts +4 -4
- package/source/conditional-keys.d.ts +3 -1
- package/source/conditional-pick.d.ts +3 -3
- package/source/delimiter-case.d.ts +2 -2
- package/source/delimiter-cased-properties-deep.d.ts +3 -1
- package/source/delimiter-cased-properties.d.ts +3 -1
- package/source/entries.d.ts +2 -2
- package/source/entry.d.ts +1 -1
- package/source/exact.d.ts +51 -0
- package/source/except.d.ts +2 -2
- package/source/fixed-length-array.d.ts +4 -1
- package/source/get.d.ts +4 -4
- package/source/includes.d.ts +2 -2
- package/source/internal.d.ts +10 -1
- package/source/invariant-of.d.ts +5 -1
- package/source/iterable-element.d.ts +6 -0
- package/source/join.d.ts +1 -1
- package/source/jsonify.d.ts +5 -1
- package/source/kebab-case.d.ts +2 -2
- package/source/kebab-cased-properties-deep.d.ts +3 -1
- package/source/kebab-cased-properties.d.ts +3 -1
- package/source/last-array-element.d.ts +1 -1
- package/source/literal-to-primitive.d.ts +1 -1
- package/source/literal-union.d.ts +2 -2
- package/source/merge-exclusive.d.ts +1 -1
- package/source/merge.d.ts +3 -3
- package/source/multidimensional-array.d.ts +2 -2
- package/source/multidimensional-readonly-array.d.ts +2 -2
- package/source/mutable.d.ts +3 -38
- package/source/numeric.d.ts +5 -5
- package/source/opaque.d.ts +1 -1
- package/source/package-json.d.ts +8 -8
- package/source/partial-deep.d.ts +2 -2
- package/source/pascal-case.d.ts +2 -2
- package/source/pascal-cased-properties-deep.d.ts +3 -1
- package/source/pascal-cased-properties.d.ts +3 -1
- package/source/promisable.d.ts +1 -1
- package/source/promise-value.d.ts +4 -4
- package/source/readonly-deep.d.ts +2 -2
- package/source/readonly-tuple.d.ts +41 -0
- package/source/remove-index-signature.d.ts +8 -2
- package/source/replace.d.ts +67 -0
- package/source/require-all-or-none.d.ts +1 -1
- package/source/require-at-least-one.d.ts +2 -2
- package/source/require-exactly-one.d.ts +1 -1
- package/source/schema.d.ts +1 -1
- package/source/screaming-snake-case.d.ts +4 -4
- package/source/set-optional.d.ts +3 -3
- package/source/set-required.d.ts +3 -3
- package/source/set-return-type.d.ts +1 -1
- package/source/simplify.d.ts +2 -2
- package/source/snake-case.d.ts +2 -2
- package/source/snake-cased-properties-deep.d.ts +3 -1
- package/source/snake-cased-properties.d.ts +3 -1
- package/source/split.d.ts +1 -1
- package/source/string-key-of.d.ts +1 -1
- package/source/stringified.d.ts +1 -1
- package/source/trim.d.ts +1 -1
- package/source/tsconfig-json.d.ts +1 -1
- package/source/union-to-intersection.d.ts +2 -2
- package/source/value-of.d.ts +1 -1
- package/source/writable.d.ts +40 -0
package/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export * from './source/observable-like';
|
|
|
7
7
|
// Utilities
|
|
8
8
|
export {Except} from './source/except';
|
|
9
9
|
export {Mutable} from './source/mutable';
|
|
10
|
+
export {Writable} from './source/writable';
|
|
10
11
|
export {Merge} from './source/merge';
|
|
11
12
|
export {MergeExclusive} from './source/merge-exclusive';
|
|
12
13
|
export {RequireAtLeastOne} from './source/require-at-least-one';
|
|
@@ -54,6 +55,8 @@ export {
|
|
|
54
55
|
NonNegativeInteger,
|
|
55
56
|
} from './source/numeric';
|
|
56
57
|
export {StringKeyOf} from './source/string-key-of';
|
|
58
|
+
export {Exact} from './source/exact';
|
|
59
|
+
export {ReadonlyTuple} from './source/readonly-tuple';
|
|
57
60
|
|
|
58
61
|
// Template literal types
|
|
59
62
|
export {CamelCase} from './source/camel-case';
|
|
@@ -75,6 +78,7 @@ export {DelimiterCasedPropertiesDeep} from './source/delimiter-cased-properties-
|
|
|
75
78
|
export {Join} from './source/join';
|
|
76
79
|
export {Split} from './source/split';
|
|
77
80
|
export {Trim} from './source/trim';
|
|
81
|
+
export {Replace} from './source/replace';
|
|
78
82
|
export {Includes} from './source/includes';
|
|
79
83
|
export {Get} from './source/get';
|
|
80
84
|
export {LastArrayElement} from './source/last-array-element';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "type-fest",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.13.0",
|
|
4
4
|
"description": "A collection of essential TypeScript types",
|
|
5
5
|
"license": "(MIT OR CC0-1.0)",
|
|
6
6
|
"repository": "sindresorhus/type-fest",
|
|
@@ -33,9 +33,9 @@
|
|
|
33
33
|
],
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@sindresorhus/tsconfig": "~0.7.0",
|
|
36
|
-
"expect-type": "^0.
|
|
37
|
-
"tsd": "^0.
|
|
38
|
-
"typescript": "
|
|
36
|
+
"expect-type": "^0.13.0",
|
|
37
|
+
"tsd": "^0.20.0",
|
|
38
|
+
"typescript": "^4.6.3",
|
|
39
39
|
"xo": "^0.43.0"
|
|
40
40
|
},
|
|
41
41
|
"types": "./index.d.ts",
|
package/readme.md
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
<br>
|
|
10
10
|
<br>
|
|
11
11
|
<br>
|
|
12
|
+
<hr>
|
|
12
13
|
<div align="center">
|
|
13
14
|
<p>
|
|
14
15
|
<p>
|
|
@@ -24,6 +25,7 @@
|
|
|
24
25
|
</a>
|
|
25
26
|
<br>
|
|
26
27
|
<br>
|
|
28
|
+
<br>
|
|
27
29
|
<a href="https://workos.com/?utm_campaign=github_repo&utm_medium=referral&utm_content=type-fest&utm_source=github">
|
|
28
30
|
<div>
|
|
29
31
|
<img src="https://sindresorhus.com/assets/thanks/workos-logo-white-bg.svg" width="220" alt="WorkOS">
|
|
@@ -35,6 +37,72 @@
|
|
|
35
37
|
<sup>Add Single Sign-On (and more) in minutes instead of months.</sup>
|
|
36
38
|
</div>
|
|
37
39
|
</a>
|
|
40
|
+
<br>
|
|
41
|
+
<br>
|
|
42
|
+
<br>
|
|
43
|
+
<a href="https://neverinstall.com/spaces/devtools?utm_source=github&utm_medium=sponsor&utm_campaign=sindre#gh-light-mode-only">
|
|
44
|
+
<div>
|
|
45
|
+
<img src="https://sindresorhus.com/assets/thanks/neverinstall-logo-light.svg" width="200" alt="neverinstall">
|
|
46
|
+
</div>
|
|
47
|
+
<br>
|
|
48
|
+
<b>All your favourite IDE's now available on the cloud</b>
|
|
49
|
+
<div>
|
|
50
|
+
<sub>
|
|
51
|
+
Neverinstall gives you an uninterrupted development experience and improved accessibility,
|
|
52
|
+
<br>
|
|
53
|
+
allowing you to code faster, better and on-the-go on your favourite IDEs like
|
|
54
|
+
<br>
|
|
55
|
+
Android Studio, VS Code, Jupyter and PyCharm using your browser.
|
|
56
|
+
</sub>
|
|
57
|
+
</div>
|
|
58
|
+
</a>
|
|
59
|
+
<a href="https://neverinstall.com/spaces/devtools?utm_source=github&utm_medium=sponsor&utm_campaign=sindre#gh-dark-mode-only">
|
|
60
|
+
<div>
|
|
61
|
+
<img src="https://sindresorhus.com/assets/thanks/neverinstall-logo-dark.svg" width="200" alt="neverinstall">
|
|
62
|
+
</div>
|
|
63
|
+
<br>
|
|
64
|
+
<b>All your favourite IDE's now available on the cloud</b>
|
|
65
|
+
<div>
|
|
66
|
+
<sub>
|
|
67
|
+
Neverinstall gives you an uninterrupted development experience and improved accessibility,
|
|
68
|
+
<br>
|
|
69
|
+
allowing you to code faster, better and on-the-go on your favourite IDEs like
|
|
70
|
+
<br>
|
|
71
|
+
Android Studio, VS Code, Jupyter and PyCharm using your browser.
|
|
72
|
+
</sub>
|
|
73
|
+
</div>
|
|
74
|
+
</a>
|
|
75
|
+
<br>
|
|
76
|
+
<br>
|
|
77
|
+
<br>
|
|
78
|
+
<a href="https://www.useanvil.com/?utm_source=sindresorhus#gh-light-mode-only">
|
|
79
|
+
<div>
|
|
80
|
+
<img src="https://sindresorhus.com/assets/thanks/anvil-logo-light.svg" width="200" alt="Anvil">
|
|
81
|
+
</div>
|
|
82
|
+
<br>
|
|
83
|
+
<b>Paperwork that makes the data work.</b>
|
|
84
|
+
<div>
|
|
85
|
+
<sub>
|
|
86
|
+
Easy APIs for paperwork. PDF generation, e-signature and embeddable no-code webforms.
|
|
87
|
+
<br>
|
|
88
|
+
The easiest way to build paperwork automation into your product.
|
|
89
|
+
</sub>
|
|
90
|
+
</div>
|
|
91
|
+
</a>
|
|
92
|
+
<a href="https://www.useanvil.com/?utm_source=sindresorhus#gh-dark-mode-only">
|
|
93
|
+
<div>
|
|
94
|
+
<img src="https://sindresorhus.com/assets/thanks/anvil-logo-dark.svg" width="200" alt="Anvil">
|
|
95
|
+
</div>
|
|
96
|
+
<br>
|
|
97
|
+
<b>Paperwork that makes the data work.</b>
|
|
98
|
+
<div>
|
|
99
|
+
<sub>
|
|
100
|
+
Easy APIs for paperwork. PDF generation, e-signature and embeddable no-code webforms.
|
|
101
|
+
<br>
|
|
102
|
+
The easiest way to build paperwork automation into your product.
|
|
103
|
+
</sub>
|
|
104
|
+
</div>
|
|
105
|
+
</a>
|
|
38
106
|
</p>
|
|
39
107
|
</div>
|
|
40
108
|
<br>
|
|
@@ -67,7 +135,7 @@ npm install type-fest
|
|
|
67
135
|
## Usage
|
|
68
136
|
|
|
69
137
|
```ts
|
|
70
|
-
import {Except} from 'type-fest';
|
|
138
|
+
import type {Except} from 'type-fest';
|
|
71
139
|
|
|
72
140
|
type Foo = {
|
|
73
141
|
unicorn: string;
|
|
@@ -93,7 +161,7 @@ Click the type names for complete docs.
|
|
|
93
161
|
### Utilities
|
|
94
162
|
|
|
95
163
|
- [`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/utility-types.html#omittype-keys).
|
|
96
|
-
- [`
|
|
164
|
+
- [`Writable`](source/writable.d.ts) - Create a type that strips `readonly` from all or some of an object's keys. The inverse of `Readonly<T>`. Formerly named `Mutable`.
|
|
97
165
|
- [`Merge`](source/merge.d.ts) - Merge two types into a new type. Keys of the second type overrides keys of the first type.
|
|
98
166
|
- [`MergeExclusive`](source/merge-exclusive.d.ts) - Create a type that has mutually exclusive keys.
|
|
99
167
|
- [`RequireAtLeastOne`](source/require-at-least-one.d.ts) - Create a type that requires at least one of the given keys.
|
|
@@ -122,6 +190,7 @@ Click the type names for complete docs.
|
|
|
122
190
|
- [`Get`](source/get.d.ts) - Get a deeply-nested property from an object using a key path, like [Lodash's `.get()`](https://lodash.com/docs/latest#get) function.
|
|
123
191
|
- [`StringKeyOf`](source/string-key-of.d.ts) - Get keys of the given type as strings.
|
|
124
192
|
- [`Schema`](source/schema.d.ts) - Create a deep version of another object type where property values are recursively replaced into a given value type.
|
|
193
|
+
- [`Exact`](source/exact.d.ts) - Create a type that does not allow extra properties.
|
|
125
194
|
|
|
126
195
|
### JSON
|
|
127
196
|
|
|
@@ -141,6 +210,7 @@ Click the type names for complete docs.
|
|
|
141
210
|
|
|
142
211
|
- [`Trim`](source/trim.d.ts) - Remove leading and trailing spaces from a string.
|
|
143
212
|
- [`Split`](source/split.d.ts) - Represents an array of strings split using a given character or character set.
|
|
213
|
+
- [`Replace`](source/replace.d.ts) - Represents a string with some or all matches replaced by a replacement.
|
|
144
214
|
|
|
145
215
|
### Array
|
|
146
216
|
|
|
@@ -150,6 +220,7 @@ Click the type names for complete docs.
|
|
|
150
220
|
- [`FixedLengthArray`](source/fixed-length-array.d.ts) - Create a type that represents an array of the given type and length.
|
|
151
221
|
- [`MultidimensionalArray`](source/multidimensional-array.d.ts) - Create a type that represents a multidimensional array of the given type and dimensions.
|
|
152
222
|
- [`MultidimensionalReadonlyArray`](source/multidimensional-readonly-array.d.ts) - Create a type that represents a multidimensional readonly array of the given type and dimensions.
|
|
223
|
+
- [`ReadonlyTuple`](source/readonly-tuple.d.ts) - Create a type that represents a read-only tuple of the given type and length.
|
|
153
224
|
|
|
154
225
|
### Numeric
|
|
155
226
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {PromiseValue} from './promise-value';
|
|
1
|
+
import type {PromiseValue} from './promise-value';
|
|
2
2
|
|
|
3
3
|
type AsyncFunction = (...args: any[]) => Promise<unknown>;
|
|
4
4
|
|
|
@@ -9,7 +9,7 @@ There has been [discussion](https://github.com/microsoft/TypeScript/pull/35998)
|
|
|
9
9
|
|
|
10
10
|
@example
|
|
11
11
|
```ts
|
|
12
|
-
import {AsyncReturnType} from 'type-fest';
|
|
12
|
+
import type {AsyncReturnType} from 'type-fest';
|
|
13
13
|
import {asyncFunction} from 'api';
|
|
14
14
|
|
|
15
15
|
// This type resolves to the unwrapped return type of `asyncFunction`.
|
package/source/asyncify.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {PromiseValue} from './promise-value';
|
|
2
|
-
import {SetReturnType} from './set-return-type';
|
|
1
|
+
import type {PromiseValue} from './promise-value';
|
|
2
|
+
import type {SetReturnType} from './set-return-type';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
Create an async version of the given function type, by boxing the return type in `Promise` while keeping the same parameter types.
|
|
@@ -8,7 +8,7 @@ Use-case: You have two functions, one synchronous and one asynchronous that do t
|
|
|
8
8
|
|
|
9
9
|
@example
|
|
10
10
|
```
|
|
11
|
-
import {Asyncify} from 'type-fest';
|
|
11
|
+
import type {Asyncify} from 'type-fest';
|
|
12
12
|
|
|
13
13
|
// Synchronous function.
|
|
14
14
|
function getFooSync(someArg: SomeType): Foo {
|
package/source/camel-case.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {WordSeparators} from '../source/utilities';
|
|
2
|
-
import {Split} from './split';
|
|
1
|
+
import type {WordSeparators} from '../source/utilities';
|
|
2
|
+
import type {Split} from './split';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
Step by step takes the first item in an array literal, formats it and adds it to a string literal, and then recursively appends the remainder.
|
|
@@ -36,7 +36,7 @@ This can be useful when, for example, converting some kebab-cased command-line f
|
|
|
36
36
|
|
|
37
37
|
@example
|
|
38
38
|
```
|
|
39
|
-
import {CamelCase} from 'type-fest';
|
|
39
|
+
import type {CamelCase} from 'type-fest';
|
|
40
40
|
|
|
41
41
|
// Simple
|
|
42
42
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {CamelCase} from './camel-case';
|
|
1
|
+
import type {CamelCase} from './camel-case';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Convert object properties to camel case recursively.
|
|
@@ -10,6 +10,8 @@ This can be useful when, for example, converting some API types from a different
|
|
|
10
10
|
|
|
11
11
|
@example
|
|
12
12
|
```
|
|
13
|
+
import type {CamelCasedPropertiesDeep} from 'type-fest';
|
|
14
|
+
|
|
13
15
|
interface User {
|
|
14
16
|
UserId: number;
|
|
15
17
|
UserName: string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {CamelCase} from './camel-case';
|
|
1
|
+
import type {CamelCase} from './camel-case';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Convert object properties to camel case but not recursively.
|
|
@@ -10,6 +10,8 @@ This can be useful when, for example, converting some API types from a different
|
|
|
10
10
|
|
|
11
11
|
@example
|
|
12
12
|
```
|
|
13
|
+
import type {CamelCasedProperties} from 'type-fest';
|
|
14
|
+
|
|
13
15
|
interface User {
|
|
14
16
|
UserId: number;
|
|
15
17
|
UserName: string;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {Except} from './except';
|
|
2
|
-
import {ConditionalKeys} from './conditional-keys';
|
|
1
|
+
import type {Except} from './except';
|
|
2
|
+
import type {ConditionalKeys} from './conditional-keys';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
Exclude keys from a shape that matches the given `Condition`.
|
|
@@ -8,7 +8,7 @@ This is useful when you want to create a new type with a specific set of keys fr
|
|
|
8
8
|
|
|
9
9
|
@example
|
|
10
10
|
```
|
|
11
|
-
import {Primitive, ConditionalExcept} from 'type-fest';
|
|
11
|
+
import type {Primitive, ConditionalExcept} from 'type-fest';
|
|
12
12
|
|
|
13
13
|
class Awesome {
|
|
14
14
|
name: string;
|
|
@@ -24,7 +24,7 @@ type ExceptPrimitivesFromAwesome = ConditionalExcept<Awesome, Primitive>;
|
|
|
24
24
|
|
|
25
25
|
@example
|
|
26
26
|
```
|
|
27
|
-
import {ConditionalExcept} from 'type-fest';
|
|
27
|
+
import type {ConditionalExcept} from 'type-fest';
|
|
28
28
|
|
|
29
29
|
interface Example {
|
|
30
30
|
a: string;
|
|
@@ -5,7 +5,7 @@ Internally this is used for the `ConditionalPick` and `ConditionalExcept` types.
|
|
|
5
5
|
|
|
6
6
|
@example
|
|
7
7
|
```
|
|
8
|
-
import {ConditionalKeys} from 'type-fest';
|
|
8
|
+
import type {ConditionalKeys} from 'type-fest';
|
|
9
9
|
|
|
10
10
|
interface Example {
|
|
11
11
|
a: string;
|
|
@@ -22,6 +22,8 @@ To support partial types, make sure your `Condition` is a union of undefined (fo
|
|
|
22
22
|
|
|
23
23
|
@example
|
|
24
24
|
```
|
|
25
|
+
import type {ConditionalKeys} from 'type-fest';
|
|
26
|
+
|
|
25
27
|
type StringKeysAndUndefined = ConditionalKeys<Example, string | undefined>;
|
|
26
28
|
//=> 'a' | 'c'
|
|
27
29
|
```
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {ConditionalKeys} from './conditional-keys';
|
|
1
|
+
import type {ConditionalKeys} from './conditional-keys';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Pick keys from the shape that matches the given `Condition`.
|
|
@@ -7,7 +7,7 @@ This is useful when you want to create a new type from a specific subset of an e
|
|
|
7
7
|
|
|
8
8
|
@example
|
|
9
9
|
```
|
|
10
|
-
import {Primitive, ConditionalPick} from 'type-fest';
|
|
10
|
+
import type {Primitive, ConditionalPick} from 'type-fest';
|
|
11
11
|
|
|
12
12
|
class Awesome {
|
|
13
13
|
name: string;
|
|
@@ -23,7 +23,7 @@ type PickPrimitivesFromAwesome = ConditionalPick<Awesome, Primitive>;
|
|
|
23
23
|
|
|
24
24
|
@example
|
|
25
25
|
```
|
|
26
|
-
import {ConditionalPick} from 'type-fest';
|
|
26
|
+
import type {ConditionalPick} from 'type-fest';
|
|
27
27
|
|
|
28
28
|
interface Example {
|
|
29
29
|
a: string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {UpperCaseCharacters, WordSeparators} from '../source/utilities';
|
|
1
|
+
import type {UpperCaseCharacters, WordSeparators} from '../source/utilities';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Unlike a simpler split, this one includes the delimiter splitted on in the resulting array literal. This is to enable splitting on, for example, upper-case characters.
|
|
@@ -54,7 +54,7 @@ This can be useful when, for example, converting a camel-cased object property t
|
|
|
54
54
|
|
|
55
55
|
@example
|
|
56
56
|
```
|
|
57
|
-
import {DelimiterCase} from 'type-fest';
|
|
57
|
+
import type {DelimiterCase} from 'type-fest';
|
|
58
58
|
|
|
59
59
|
// Simple
|
|
60
60
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {DelimiterCase} from './delimiter-case';
|
|
1
|
+
import type {DelimiterCase} from './delimiter-case';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Convert object properties to delimiter case recursively.
|
|
@@ -10,6 +10,8 @@ This can be useful when, for example, converting some API types from a different
|
|
|
10
10
|
|
|
11
11
|
@example
|
|
12
12
|
```
|
|
13
|
+
import type {DelimiterCasedPropertiesDeep} from 'type-fest';
|
|
14
|
+
|
|
13
15
|
interface User {
|
|
14
16
|
userId: number;
|
|
15
17
|
userName: string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {DelimiterCase} from './delimiter-case';
|
|
1
|
+
import type {DelimiterCase} from './delimiter-case';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Convert object properties to delimiter case but not recursively.
|
|
@@ -10,6 +10,8 @@ This can be useful when, for example, converting some API types from a different
|
|
|
10
10
|
|
|
11
11
|
@example
|
|
12
12
|
```
|
|
13
|
+
import type {DelimiterCasedProperties} from 'type-fest';
|
|
14
|
+
|
|
13
15
|
interface User {
|
|
14
16
|
userId: number;
|
|
15
17
|
userName: string;
|
package/source/entries.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {ArrayEntry, MapEntry, ObjectEntry, SetEntry} from './entry';
|
|
1
|
+
import type {ArrayEntry, MapEntry, ObjectEntry, SetEntry} from './entry';
|
|
2
2
|
|
|
3
3
|
type ArrayEntries<BaseType extends readonly unknown[]> = Array<ArrayEntry<BaseType>>;
|
|
4
4
|
type MapEntries<BaseType> = Array<MapEntry<BaseType>>;
|
|
@@ -14,7 +14,7 @@ For example the {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/R
|
|
|
14
14
|
|
|
15
15
|
@example
|
|
16
16
|
```
|
|
17
|
-
import {Entries} from 'type-fest';
|
|
17
|
+
import type {Entries} from 'type-fest';
|
|
18
18
|
|
|
19
19
|
interface Example {
|
|
20
20
|
someKey: number;
|
package/source/entry.d.ts
CHANGED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type {Primitive} from './primitive';
|
|
2
|
+
import type {KeysOfUnion} from './internal';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
Create a type that does not allow extra properties, meaning it only allows properties that are explicitly declared.
|
|
6
|
+
|
|
7
|
+
This is useful for function type-guarding to reject arguments with excess properties. Due to the nature of TypeScript, it does not complain if excess properties are provided unless the provided value is an object literal.
|
|
8
|
+
|
|
9
|
+
*Please upvote [this issue](https://github.com/microsoft/TypeScript/issues/12936) if you want to have this type as a built-in in TypeScript.*
|
|
10
|
+
|
|
11
|
+
@example
|
|
12
|
+
```
|
|
13
|
+
type OnlyAcceptName = {name: string};
|
|
14
|
+
|
|
15
|
+
function onlyAcceptName(args: OnlyAcceptName) {}
|
|
16
|
+
|
|
17
|
+
// TypeScript complains about excess properties when an object literal is provided.
|
|
18
|
+
onlyAcceptName({name: 'name', id: 1});
|
|
19
|
+
//=> `id` is excess
|
|
20
|
+
|
|
21
|
+
// TypeScript does not complain about excess properties when the provided value is a variable (not an object literal).
|
|
22
|
+
const invalidInput = {name: 'name', id: 1};
|
|
23
|
+
onlyAcceptName(invalidInput); // No errors
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Having `Exact` allows TypeScript to reject excess properties.
|
|
27
|
+
|
|
28
|
+
@example
|
|
29
|
+
```
|
|
30
|
+
import {Exact} from 'type-fest';
|
|
31
|
+
|
|
32
|
+
type OnlyAcceptName = {name: string};
|
|
33
|
+
|
|
34
|
+
function onlyAcceptNameImproved<T extends Exact<OnlyAcceptName, T>>(args: T) {}
|
|
35
|
+
|
|
36
|
+
const invalidInput = {name: 'name', id: 1};
|
|
37
|
+
onlyAcceptNameImproved(invalidInput); // Compilation error
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
[Read more](https://stackoverflow.com/questions/49580725/is-it-possible-to-restrict-typescript-object-to-contain-only-properties-defined)
|
|
41
|
+
|
|
42
|
+
@category Utilities
|
|
43
|
+
*/
|
|
44
|
+
export type Exact<ParameterType, InputType extends ParameterType> = ParameterType extends Primitive
|
|
45
|
+
? ParameterType
|
|
46
|
+
/*
|
|
47
|
+
Create a type from `ParameterType` and `InputType` and change keys exclusive to `InputType` to `never`.
|
|
48
|
+
- Generate a list of keys that exists in `InputType` but not in `ParameterType`.
|
|
49
|
+
- Mark these excess keys as `never`.
|
|
50
|
+
*/
|
|
51
|
+
: {[Key in keyof ParameterType]: Exact<ParameterType[Key], InputType[Key]>} & Record<Exclude<keyof InputType, KeysOfUnion<ParameterType>>, never>;
|
package/source/except.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {IsEqual} from './internal';
|
|
1
|
+
import type {IsEqual} from './internal';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Filter out keys from an object.
|
|
@@ -38,7 +38,7 @@ This type was proposed to the TypeScript team, which declined it, saying they pr
|
|
|
38
38
|
|
|
39
39
|
@example
|
|
40
40
|
```
|
|
41
|
-
import {Except} from 'type-fest';
|
|
41
|
+
import type {Except} from 'type-fest';
|
|
42
42
|
|
|
43
43
|
type Foo = {
|
|
44
44
|
a: number;
|
|
@@ -13,9 +13,11 @@ Use-cases:
|
|
|
13
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
14
|
- Creating an array of coordinates with a static length, for example, length of 3 for a 3D vector.
|
|
15
15
|
|
|
16
|
+
Note: This type does not prevent out-of-bounds access. Prefer `ReadonlyTuple` unless you need mutability.
|
|
17
|
+
|
|
16
18
|
@example
|
|
17
19
|
```
|
|
18
|
-
import {FixedLengthArray} from 'type-fest';
|
|
20
|
+
import type {FixedLengthArray} from 'type-fest';
|
|
19
21
|
|
|
20
22
|
type FencingTeam = FixedLengthArray<string, 3>;
|
|
21
23
|
|
|
@@ -29,6 +31,7 @@ guestFencingTeam.push('Sam');
|
|
|
29
31
|
```
|
|
30
32
|
|
|
31
33
|
@category Array
|
|
34
|
+
@see ReadonlyTuple
|
|
32
35
|
*/
|
|
33
36
|
export type FixedLengthArray<Element, Length extends number, ArrayPrototype = [Element, ...Element[]]> = Pick<
|
|
34
37
|
ArrayPrototype,
|
package/source/get.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {StringDigit} from '../source/utilities';
|
|
2
|
-
import {Split} from './split';
|
|
3
|
-
import {StringKeyOf} from './string-key-of';
|
|
1
|
+
import type {StringDigit} from '../source/utilities';
|
|
2
|
+
import type {Split} from './split';
|
|
3
|
+
import type {StringKeyOf} from './string-key-of';
|
|
4
4
|
|
|
5
5
|
type GetOptions = {
|
|
6
6
|
strict?: boolean;
|
|
@@ -141,7 +141,7 @@ Use-case: Retrieve a property from deep inside an API response or some other com
|
|
|
141
141
|
|
|
142
142
|
@example
|
|
143
143
|
```
|
|
144
|
-
import {Get} from 'type-fest';
|
|
144
|
+
import type {Get} from 'type-fest';
|
|
145
145
|
import * as lodash from 'lodash';
|
|
146
146
|
|
|
147
147
|
const get = <BaseType, Path extends string | readonly string[]>(object: BaseType, path: Path): Get<BaseType, Path> =>
|
package/source/includes.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {IsEqual} from './internal';
|
|
1
|
+
import type {IsEqual} from './internal';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Returns a boolean for whether the given array includes the given item.
|
|
@@ -7,7 +7,7 @@ This can be useful if another type wants to make a decision based on whether the
|
|
|
7
7
|
|
|
8
8
|
@example
|
|
9
9
|
```
|
|
10
|
-
import {Includes} from 'type-fest';
|
|
10
|
+
import type {Includes} from 'type-fest';
|
|
11
11
|
|
|
12
12
|
type hasRed<array extends any[]> = Includes<array, 'red'>;
|
|
13
13
|
```
|
package/source/internal.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {Primitive} from './primitive';
|
|
1
|
+
import type {Primitive} from './primitive';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Returns a boolean for whether the two given types are equal.
|
|
@@ -42,3 +42,12 @@ export type Subtract<A extends number, B extends number> = BuildTuple<A> extends
|
|
|
42
42
|
Matches any primitive, `Date`, or `RegExp` value.
|
|
43
43
|
*/
|
|
44
44
|
export type BuiltIns = Primitive | Date | RegExp;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
Gets keys from a type. Similar to `keyof` but this one also works for union types.
|
|
48
|
+
|
|
49
|
+
The reason a simple `keyof Union` does not work is because `keyof` always returns the accessible keys of a type. In the case of a union, that will only be the common keys.
|
|
50
|
+
|
|
51
|
+
@link https://stackoverflow.com/a/49402091
|
|
52
|
+
*/
|
|
53
|
+
export type KeysOfUnion<T> = T extends T ? keyof T : never;
|
package/source/invariant-of.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {Opaque} from './opaque';
|
|
1
|
+
import type {Opaque} from './opaque';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Create an [invariant type](https://basarat.gitbook.io/typescript/type-system/type-compatibility#footnote-invariance), which is a type that does not accept supertypes and subtypes.
|
|
@@ -9,6 +9,8 @@ Use-case:
|
|
|
9
9
|
|
|
10
10
|
@example
|
|
11
11
|
```
|
|
12
|
+
import type {InvariantOf} from 'type-fest';
|
|
13
|
+
|
|
12
14
|
class Animal {
|
|
13
15
|
constructor(public name: string){}
|
|
14
16
|
}
|
|
@@ -32,6 +34,8 @@ invariantAnimalArray = invariantCatArray; // Error: Type 'InvariantOf<Cat>[]' is
|
|
|
32
34
|
|
|
33
35
|
@example
|
|
34
36
|
```
|
|
37
|
+
import type {InvariantOf} from 'type-fest';
|
|
38
|
+
|
|
35
39
|
// In covariance (default)
|
|
36
40
|
|
|
37
41
|
interface FooBar {
|
|
@@ -9,6 +9,8 @@ Here is an example of `IterableElement` in action with a generator function:
|
|
|
9
9
|
|
|
10
10
|
@example
|
|
11
11
|
```
|
|
12
|
+
import type {IterableElement} from 'type-fest';
|
|
13
|
+
|
|
12
14
|
function * iAmGenerator() {
|
|
13
15
|
yield 1;
|
|
14
16
|
yield 2;
|
|
@@ -21,6 +23,8 @@ And here is an example with an async generator:
|
|
|
21
23
|
|
|
22
24
|
@example
|
|
23
25
|
```
|
|
26
|
+
import type {IterableElement} from 'type-fest';
|
|
27
|
+
|
|
24
28
|
async function * iAmGeneratorAsync() {
|
|
25
29
|
yield 'hi';
|
|
26
30
|
yield true;
|
|
@@ -35,6 +39,8 @@ An example with an array of strings:
|
|
|
35
39
|
|
|
36
40
|
@example
|
|
37
41
|
```
|
|
42
|
+
import type {IterableElement} from 'type-fest';
|
|
43
|
+
|
|
38
44
|
type MeString = IterableElement<string[]>
|
|
39
45
|
```
|
|
40
46
|
|
package/source/join.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ Use-case: Defining key paths in a nested object. For example, for dot-notation f
|
|
|
5
5
|
|
|
6
6
|
@example
|
|
7
7
|
```
|
|
8
|
-
import {Join} from 'type-fest';
|
|
8
|
+
import type {Join} from 'type-fest';
|
|
9
9
|
|
|
10
10
|
// Mixed (strings & numbers) items; result is: 'foo.0.baz'
|
|
11
11
|
const path: Join<['foo', 0, 'baz'], '.'> = ['foo', 0, 'baz'].join('.');
|
package/source/jsonify.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {JsonPrimitive, JsonValue} from './basic';
|
|
1
|
+
import type {JsonPrimitive, JsonValue} from './basic';
|
|
2
2
|
|
|
3
3
|
// Note: The return value has to be `any` and not `unknown` so it can match `void`.
|
|
4
4
|
type NotJsonable = ((...args: any[]) => any) | undefined;
|
|
@@ -23,6 +23,8 @@ An interface cannot be structurally compared to `JsonValue` because an interface
|
|
|
23
23
|
|
|
24
24
|
@example
|
|
25
25
|
```
|
|
26
|
+
import type {Jsonify} from 'type-fest';
|
|
27
|
+
|
|
26
28
|
interface Geometry {
|
|
27
29
|
type: 'Point' | 'Polygon';
|
|
28
30
|
coordinates: [number, number];
|
|
@@ -51,6 +53,8 @@ Non-JSON values such as `Date` implement `.toJSON()`, so they can be transformed
|
|
|
51
53
|
|
|
52
54
|
@example
|
|
53
55
|
```
|
|
56
|
+
import type {Jsonify} from 'type-fest';
|
|
57
|
+
|
|
54
58
|
const time = {
|
|
55
59
|
timeValue: new Date()
|
|
56
60
|
};
|
package/source/kebab-case.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {DelimiterCase} from './delimiter-case';
|
|
1
|
+
import type {DelimiterCase} from './delimiter-case';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Convert a string literal to kebab-case.
|
|
@@ -7,7 +7,7 @@ This can be useful when, for example, converting a camel-cased object property t
|
|
|
7
7
|
|
|
8
8
|
@example
|
|
9
9
|
```
|
|
10
|
-
import {KebabCase} from 'type-fest';
|
|
10
|
+
import type {KebabCase} from 'type-fest';
|
|
11
11
|
|
|
12
12
|
// Simple
|
|
13
13
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {DelimiterCasedPropertiesDeep} from './delimiter-cased-properties-deep';
|
|
1
|
+
import type {DelimiterCasedPropertiesDeep} from './delimiter-cased-properties-deep';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
Convert object properties to kebab case recursively.
|
|
@@ -10,6 +10,8 @@ This can be useful when, for example, converting some API types from a different
|
|
|
10
10
|
|
|
11
11
|
@example
|
|
12
12
|
```
|
|
13
|
+
import type [KebabCasedPropertiesDeep] from 'type-fest';
|
|
14
|
+
|
|
13
15
|
interface User {
|
|
14
16
|
userId: number;
|
|
15
17
|
userName: string;
|