type-fest 4.0.0 → 4.2.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 +2 -0
- package/package.json +1 -1
- package/readme.md +14 -0
- package/source/internal.d.ts +8 -5
- package/source/merge-deep.d.ts +1 -1
- package/source/readonly-deep.d.ts +12 -3
- package/source/require-all-or-none.d.ts +10 -4
- package/source/require-one-or-none.d.ts +37 -0
- package/source/unknown-record.d.ts +31 -0
package/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export * from './source/observable-like';
|
|
|
6
6
|
|
|
7
7
|
// Utilities
|
|
8
8
|
export type {EmptyObject, IsEmptyObject} from './source/empty-object';
|
|
9
|
+
export type {UnknownRecord} from './source/unknown-record';
|
|
9
10
|
export type {Except} from './source/except';
|
|
10
11
|
export type {TaggedUnion} from './source/tagged-union';
|
|
11
12
|
export type {Writable} from './source/writable';
|
|
@@ -16,6 +17,7 @@ export type {MergeExclusive} from './source/merge-exclusive';
|
|
|
16
17
|
export type {RequireAtLeastOne} from './source/require-at-least-one';
|
|
17
18
|
export type {RequireExactlyOne} from './source/require-exactly-one';
|
|
18
19
|
export type {RequireAllOrNone} from './source/require-all-or-none';
|
|
20
|
+
export type {RequireOneOrNone} from './source/require-one-or-none';
|
|
19
21
|
export type {OmitIndexSignature} from './source/omit-index-signature';
|
|
20
22
|
export type {PickIndexSignature} from './source/pick-index-signature';
|
|
21
23
|
export type {PartialDeep, PartialDeepOptions} from './source/partial-deep';
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -37,6 +37,18 @@
|
|
|
37
37
|
<sup>Add Single Sign-On (and more) in minutes instead of months.</sup>
|
|
38
38
|
</div>
|
|
39
39
|
</a>
|
|
40
|
+
<br>
|
|
41
|
+
<br>
|
|
42
|
+
<br>
|
|
43
|
+
<a href="https://transloadit.com?utm_source=sindresorhus&utm_medium=referral&utm_campaign=sponsorship&utm_content=type-fest">
|
|
44
|
+
<picture>
|
|
45
|
+
<source width="350" media="(prefers-color-scheme: dark)" srcset="https://sindresorhus.com/assets/thanks/transloadit-logo-dark.svg">
|
|
46
|
+
<source width="350" media="(prefers-color-scheme: light)" srcset="https://sindresorhus.com/assets/thanks/transloadit-logo.svg">
|
|
47
|
+
<img width="350" src="https://sindresorhus.com/assets/thanks/transloadit-logo.svg" alt="Transloadit logo">
|
|
48
|
+
</picture>
|
|
49
|
+
</a>
|
|
50
|
+
<br>
|
|
51
|
+
<br>
|
|
40
52
|
</p>
|
|
41
53
|
</div>
|
|
42
54
|
<br>
|
|
@@ -98,6 +110,7 @@ Click the type names for complete docs.
|
|
|
98
110
|
|
|
99
111
|
- [`EmptyObject`](source/empty-object.d.ts) - Represents a strictly empty plain object, the `{}` value.
|
|
100
112
|
- [`IsEmptyObject`](source/empty-object.d.ts) - Returns a `boolean` for whether the type is strictly equal to an empty plain object, the `{}` value.
|
|
113
|
+
- [`UnknownRecord`](source/unknown-record.d.ts) - Represents an object with `unknown` value. You probably want this instead of `{}`.
|
|
101
114
|
- [`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).
|
|
102
115
|
- [`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>`.
|
|
103
116
|
- [`WritableDeep`](source/writable-deep.d.ts) - Create a deeply mutable version of an `object`/`ReadonlyMap`/`ReadonlySet`/`ReadonlyArray` type. The inverse of `ReadonlyDeep<T>`. Use `Writable<T>` if you only need one level deep.
|
|
@@ -108,6 +121,7 @@ Click the type names for complete docs.
|
|
|
108
121
|
- [`RequireAtLeastOne`](source/require-at-least-one.d.ts) - Create a type that requires at least one of the given keys.
|
|
109
122
|
- [`RequireExactlyOne`](source/require-exactly-one.d.ts) - Create a type that requires exactly a single key of the given keys and disallows more.
|
|
110
123
|
- [`RequireAllOrNone`](source/require-all-or-none.d.ts) - Create a type that requires all of the given keys or none of the given keys.
|
|
124
|
+
- [`RequireOneOrNone`](source/require-one-or-none.d.ts) - Create a type that requires exactly a single key of the given keys and disallows more, or none of the given keys.
|
|
111
125
|
- [`RequiredDeep`](source/required-deep.d.ts) - Create a deeply required version of another type. Use [`Required<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#requiredtype) if you only need one level deep.
|
|
112
126
|
- [`OmitIndexSignature`](source/omit-index-signature.d.ts) - Omit any index signatures from the given object type, leaving only explicitly defined properties.
|
|
113
127
|
- [`PickIndexSignature`](source/pick-index-signature.d.ts) - Pick only index signatures from the given object type, leaving out all explicitly defined properties.
|
package/source/internal.d.ts
CHANGED
|
@@ -3,6 +3,9 @@ import type {Simplify} from './simplify';
|
|
|
3
3
|
import type {Trim} from './trim';
|
|
4
4
|
import type {IsAny} from './is-any';
|
|
5
5
|
|
|
6
|
+
// TODO: Remove for v5.
|
|
7
|
+
export type {UnknownRecord} from './unknown-record';
|
|
8
|
+
|
|
6
9
|
/**
|
|
7
10
|
Infer the length of the given array `<T>`.
|
|
8
11
|
|
|
@@ -77,11 +80,6 @@ export type Whitespace =
|
|
|
77
80
|
|
|
78
81
|
export type WordSeparators = '-' | '_' | Whitespace;
|
|
79
82
|
|
|
80
|
-
/**
|
|
81
|
-
Matches any unknown record.
|
|
82
|
-
*/
|
|
83
|
-
export type UnknownRecord = Record<PropertyKey, unknown>;
|
|
84
|
-
|
|
85
83
|
/**
|
|
86
84
|
Matches any unknown array or tuple.
|
|
87
85
|
*/
|
|
@@ -251,3 +249,8 @@ export type IsNotFalse<T extends boolean> = [T] extends [false] ? false : true;
|
|
|
251
249
|
Returns a boolean for whether the given type is `null`.
|
|
252
250
|
*/
|
|
253
251
|
export type IsNull<T> = [T] extends [null] ? true : false;
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
Disallows any of the given keys.
|
|
255
|
+
*/
|
|
256
|
+
export type RequireNone<KeysType extends PropertyKey> = Partial<Record<KeysType, never>>;
|
package/source/merge-deep.d.ts
CHANGED
|
@@ -9,8 +9,8 @@ import type {
|
|
|
9
9
|
IsBothExtends,
|
|
10
10
|
NonEmptyTuple,
|
|
11
11
|
UnknownArrayOrTuple,
|
|
12
|
-
UnknownRecord,
|
|
13
12
|
} from './internal';
|
|
13
|
+
import type {UnknownRecord} from './unknown-record';
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
Deeply simplifies an object excluding iterables and functions. Used internally to improve the UX and accept both interfaces and type aliases as inputs.
|
|
@@ -48,9 +48,18 @@ export type ReadonlyDeep<T> = T extends BuiltIns
|
|
|
48
48
|
? ReadonlyMapDeep<KeyType, ValueType>
|
|
49
49
|
: T extends Readonly<ReadonlySet<infer ItemType>>
|
|
50
50
|
? ReadonlySetDeep<ItemType>
|
|
51
|
-
:
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
: // Identify tuples to avoid converting them to arrays inadvertently; special case `readonly [...never[]]`, as it emerges undesirably from recursive invocations of ReadonlyDeep below.
|
|
52
|
+
T extends readonly [] | readonly [...never[]]
|
|
53
|
+
? readonly []
|
|
54
|
+
: T extends readonly [infer U, ...infer V]
|
|
55
|
+
? readonly [ReadonlyDeep<U>, ...ReadonlyDeep<V>]
|
|
56
|
+
: T extends readonly [...infer U, infer V]
|
|
57
|
+
? readonly [...ReadonlyDeep<U>, ReadonlyDeep<V>]
|
|
58
|
+
: T extends ReadonlyArray<infer ItemType>
|
|
59
|
+
? ReadonlyArray<ReadonlyDeep<ItemType>>
|
|
60
|
+
: T extends object
|
|
61
|
+
? ReadonlyObjectDeep<T>
|
|
62
|
+
: unknown;
|
|
54
63
|
|
|
55
64
|
/**
|
|
56
65
|
Same as `ReadonlyDeep`, but accepts only `ReadonlyMap`s as inputs. Internal helper for `ReadonlyDeep`.
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
import type {RequireNone} from './internal';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Requires all of the keys in the given object.
|
|
5
|
+
*/
|
|
6
|
+
type RequireAll<ObjectType, KeysType extends keyof ObjectType> = Required<Pick<ObjectType, KeysType>>;
|
|
7
|
+
|
|
1
8
|
/**
|
|
2
9
|
Create a type that requires all of the given keys or none of the given keys. The remaining keys are kept as is.
|
|
3
10
|
|
|
@@ -30,7 +37,6 @@ const responder2: RequireAllOrNone<Responder, 'text' | 'json'> = {
|
|
|
30
37
|
@category Object
|
|
31
38
|
*/
|
|
32
39
|
export type RequireAllOrNone<ObjectType, KeysType extends keyof ObjectType = never> = (
|
|
33
|
-
|
|
|
34
|
-
|
|
|
35
|
-
) &
|
|
36
|
-
Omit<ObjectType, KeysType>; // The rest of the keys.
|
|
40
|
+
| RequireAll<ObjectType, KeysType>
|
|
41
|
+
| RequireNone<KeysType>
|
|
42
|
+
) & Omit<ObjectType, KeysType>; // The rest of the keys.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type {RequireExactlyOne} from './require-exactly-one';
|
|
2
|
+
import type {RequireNone} from './internal';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
Create a type that requires exactly one of the given keys and disallows more, or none of the given keys. The remaining keys are kept as is.
|
|
6
|
+
|
|
7
|
+
@example
|
|
8
|
+
```
|
|
9
|
+
import type {RequireOneOrNone} from 'type-fest';
|
|
10
|
+
|
|
11
|
+
type Responder = RequireOneOrNone<{
|
|
12
|
+
text: () => string;
|
|
13
|
+
json: () => string;
|
|
14
|
+
secure: boolean;
|
|
15
|
+
}, 'text' | 'json'>;
|
|
16
|
+
|
|
17
|
+
const responder1: Responder = {
|
|
18
|
+
secure: true
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const responder2: Responder = {
|
|
22
|
+
text: () => '{"message": "hi"}',
|
|
23
|
+
secure: true
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const responder3: Responder = {
|
|
27
|
+
json: () => '{"message": "ok"}',
|
|
28
|
+
secure: true
|
|
29
|
+
};
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
@category Object
|
|
33
|
+
*/
|
|
34
|
+
export type RequireOneOrNone<ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> = (
|
|
35
|
+
| RequireExactlyOne<ObjectType, KeysType>
|
|
36
|
+
| RequireNone<KeysType>
|
|
37
|
+
) & Omit<ObjectType, KeysType>; // Ignore unspecified keys.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Represents an object with `unknown` value. You probably want this instead of `{}`.
|
|
3
|
+
|
|
4
|
+
Use case: You have an object whose keys and values are unknown to you.
|
|
5
|
+
|
|
6
|
+
@example
|
|
7
|
+
```
|
|
8
|
+
import type {UnknownRecord} from 'type-fest';
|
|
9
|
+
|
|
10
|
+
function toJson(object: UnknownRecord) {
|
|
11
|
+
return JSON.stringify(object);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
toJson({hello: 'world'});
|
|
15
|
+
//=> '{"hello":"world"}'
|
|
16
|
+
|
|
17
|
+
function isObject(value: unknown): value is UnknownRecord {
|
|
18
|
+
return typeof value === 'object' && value !== null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
isObject({hello: 'world'});
|
|
22
|
+
//=> true
|
|
23
|
+
|
|
24
|
+
isObject('hello');
|
|
25
|
+
//=> false
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
@category Type
|
|
29
|
+
@category Object
|
|
30
|
+
*/
|
|
31
|
+
export type UnknownRecord = Record<PropertyKey, unknown>;
|