type-fest 3.12.0 → 3.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 +2 -0
- package/package.json +1 -1
- package/readme.md +4 -2
- package/source/basic.d.ts +11 -2
- package/source/has-readonly-keys.d.ts +21 -0
- package/source/has-writable-keys.d.ts +21 -0
- package/source/jsonify.d.ts +15 -5
package/index.d.ts
CHANGED
|
@@ -79,7 +79,9 @@ export type {HasOptionalKeys} from './source/has-optional-keys';
|
|
|
79
79
|
export type {RequiredKeysOf} from './source/required-keys-of';
|
|
80
80
|
export type {HasRequiredKeys} from './source/has-required-keys';
|
|
81
81
|
export type {ReadonlyKeysOf} from './source/readonly-keys-of';
|
|
82
|
+
export type {HasReadonlyKeys} from './source/has-readonly-keys';
|
|
82
83
|
export type {WritableKeysOf} from './source/writable-keys-of';
|
|
84
|
+
export type {HasWritableKeys} from './source/has-writable-keys';
|
|
83
85
|
export type {Spread} from './source/spread';
|
|
84
86
|
export type {TupleToUnion} from './source/tuple-to-union';
|
|
85
87
|
export type {IsEqual} from './source/is-equal';
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -144,8 +144,10 @@ Click the type names for complete docs.
|
|
|
144
144
|
- [`HasOptionalKeys`](source/has-optional-keys.d.ts) - Create a `true`/`false` type depending on whether the given type has any optional fields.
|
|
145
145
|
- [`RequiredKeysOf`](source/required-keys-of.d.ts) - Extract all required keys from the given type.
|
|
146
146
|
- [`HasRequiredKeys`](source/has-required-keys.d.ts) - Create a `true`/`false` type depending on whether the given type has any required fields.
|
|
147
|
-
- [`ReadonlyKeysOf`](source/readonly-keys-of.d.ts) - Extract all
|
|
148
|
-
- [`
|
|
147
|
+
- [`ReadonlyKeysOf`](source/readonly-keys-of.d.ts) - Extract all readonly keys from the given type.
|
|
148
|
+
- [`HasReadonlyKeys`](source/has-readonly-keys.d.ts) - Create a `true`/`false` type depending on whether the given type has any readonly fields.
|
|
149
|
+
- [`WritableKeysOf`](source/writable-keys-of.d.ts) - Extract all writable (non-readonly) keys from the given type.
|
|
150
|
+
- [`HasWritableKeys`](source/has-writable-keys.d.ts) - Create a `true`/`false` type depending on whether the given type has any writable fields.
|
|
149
151
|
- [`Spread`](source/spread.d.ts) - Mimic the type inferred by TypeScript when merging two objects or two arrays/tuples using the spread syntax.
|
|
150
152
|
- [`IsEqual`](source/is-equal.d.ts) - Returns a boolean for whether the two given types are equal.
|
|
151
153
|
- [`TaggedUnion`](source/tagged-union.d.ts) - Create a union of types that share a common discriminant property.
|
package/source/basic.d.ts
CHANGED
|
@@ -3,7 +3,10 @@ Matches a [`class`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe
|
|
|
3
3
|
|
|
4
4
|
@category Class
|
|
5
5
|
*/
|
|
6
|
-
export type Class<T, Arguments extends unknown[] = any[]> =
|
|
6
|
+
export type Class<T, Arguments extends unknown[] = any[]> = {
|
|
7
|
+
prototype: T;
|
|
8
|
+
new(...arguments_: Arguments): T;
|
|
9
|
+
};
|
|
7
10
|
|
|
8
11
|
/**
|
|
9
12
|
Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
|
|
@@ -16,8 +19,14 @@ export type Constructor<T, Arguments extends unknown[] = any[]> = new(...argumen
|
|
|
16
19
|
Matches an [`abstract class`](https://www.typescriptlang.org/docs/handbook/classes.html#abstract-classes).
|
|
17
20
|
|
|
18
21
|
@category Class
|
|
22
|
+
|
|
23
|
+
@privateRemarks
|
|
24
|
+
We cannot use a `type` here because TypeScript throws: 'abstract' modifier cannot appear on a type member. (1070)
|
|
19
25
|
*/
|
|
20
|
-
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
27
|
+
export interface AbstractClass<T, Arguments extends unknown[] = any[]> extends AbstractConstructor<T, Arguments> {
|
|
28
|
+
prototype: T;
|
|
29
|
+
}
|
|
21
30
|
|
|
22
31
|
/**
|
|
23
32
|
Matches an [`abstract class`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-2.html#abstract-construct-signatures) constructor.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type {ReadonlyKeysOf} from './readonly-keys-of';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Creates a type that represents `true` or `false` depending on whether the given type has any readonly fields.
|
|
5
|
+
|
|
6
|
+
This is useful when you want to create an API whose behavior depends on the presence or absence of readonly fields.
|
|
7
|
+
|
|
8
|
+
@example
|
|
9
|
+
```
|
|
10
|
+
import type {HasReadonlyKeys, ReadonlyKeysOf} from 'type-fest';
|
|
11
|
+
|
|
12
|
+
type UpdateService<Entity extends object> = {
|
|
13
|
+
removeField: HasReadonlyKeys<Entity> extends true
|
|
14
|
+
? (field: ReadonlyKeysOf<Entity>) => Promise<void>
|
|
15
|
+
: never
|
|
16
|
+
}
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
@category Utilities
|
|
20
|
+
*/
|
|
21
|
+
export type HasReadonlyKeys<BaseType extends object> = ReadonlyKeysOf<BaseType> extends never ? false : true;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type {WritableKeysOf} from './writable-keys-of';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
Creates a type that represents `true` or `false` depending on whether the given type has any writable fields.
|
|
5
|
+
|
|
6
|
+
This is useful when you want to create an API whose behavior depends on the presence or absence of writable fields.
|
|
7
|
+
|
|
8
|
+
@example
|
|
9
|
+
```
|
|
10
|
+
import type {HasWritableKeys, WritableKeysOf} from 'type-fest';
|
|
11
|
+
|
|
12
|
+
type UpdateService<Entity extends object> = {
|
|
13
|
+
removeField: HasWritableKeys<Entity> extends true
|
|
14
|
+
? (field: WritableKeysOf<Entity>) => Promise<void>
|
|
15
|
+
: never
|
|
16
|
+
}
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
@category Utilities
|
|
20
|
+
*/
|
|
21
|
+
export type HasWritableKeys<BaseType extends object> = WritableKeysOf<BaseType> extends never ? false : true;
|
package/source/jsonify.d.ts
CHANGED
|
@@ -1,16 +1,26 @@
|
|
|
1
1
|
import type {JsonPrimitive, JsonValue} from './basic';
|
|
2
2
|
import type {EmptyObject} from './empty-object';
|
|
3
3
|
import type {UndefinedToOptional} from './internal';
|
|
4
|
+
import type {IsAny} from './is-any';
|
|
5
|
+
import type {IsNever} from './is-never';
|
|
4
6
|
import type {NegativeInfinity, PositiveInfinity} from './numeric';
|
|
5
7
|
import type {TypedArray} from './typed-array';
|
|
6
|
-
import type {IsAny} from './is-any';
|
|
7
8
|
|
|
8
9
|
// Note: The return value has to be `any` and not `unknown` so it can match `void`.
|
|
9
10
|
type NotJsonable = ((...arguments_: any[]) => any) | undefined | symbol;
|
|
10
11
|
|
|
11
|
-
type
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
type FilterNonNever<T extends unknown[]> = T extends [infer F, ...infer R]
|
|
13
|
+
? IsNever<F> extends true
|
|
14
|
+
? FilterNonNever<R>
|
|
15
|
+
: [F, ...FilterNonNever<R>]
|
|
16
|
+
: IsNever<T[number]> extends true
|
|
17
|
+
? []
|
|
18
|
+
: T;
|
|
19
|
+
|
|
20
|
+
// Handles tuples and arrays
|
|
21
|
+
type JsonifyList<T extends unknown[]> = T extends [infer F, ...infer R]
|
|
22
|
+
? FilterNonNever<[Jsonify<F>, ...JsonifyList<R>]>
|
|
23
|
+
: Array<Jsonify<T[number]>>;
|
|
14
24
|
|
|
15
25
|
type FilterJsonableKeys<T extends object> = {
|
|
16
26
|
[Key in keyof T]: T[Key] extends NotJsonable ? never : Key;
|
|
@@ -107,7 +117,7 @@ export type Jsonify<T> = IsAny<T> extends true
|
|
|
107
117
|
: T extends []
|
|
108
118
|
? []
|
|
109
119
|
: T extends [unknown, ...unknown[]]
|
|
110
|
-
?
|
|
120
|
+
? JsonifyList<T>
|
|
111
121
|
: T extends ReadonlyArray<infer U>
|
|
112
122
|
? Array<U extends NotJsonable ? null : Jsonify<U>>
|
|
113
123
|
: T extends object
|