type-fest 3.11.1 → 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 CHANGED
@@ -33,6 +33,7 @@ export type {Promisable} from './source/promisable';
33
33
  export type {Opaque, UnwrapOpaque} from './source/opaque';
34
34
  export type {InvariantOf} from './source/invariant-of';
35
35
  export type {SetOptional} from './source/set-optional';
36
+ export type {SetReadonly} from './source/set-readonly';
36
37
  export type {SetRequired} from './source/set-required';
37
38
  export type {SetNonNullable} from './source/set-non-nullable';
38
39
  export type {ValueOf} from './source/value-of';
@@ -78,7 +79,9 @@ export type {HasOptionalKeys} from './source/has-optional-keys';
78
79
  export type {RequiredKeysOf} from './source/required-keys-of';
79
80
  export type {HasRequiredKeys} from './source/has-required-keys';
80
81
  export type {ReadonlyKeysOf} from './source/readonly-keys-of';
82
+ export type {HasReadonlyKeys} from './source/has-readonly-keys';
81
83
  export type {WritableKeysOf} from './source/writable-keys-of';
84
+ export type {HasWritableKeys} from './source/has-writable-keys';
82
85
  export type {Spread} from './source/spread';
83
86
  export type {TupleToUnion} from './source/tuple-to-union';
84
87
  export type {IsEqual} from './source/is-equal';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-fest",
3
- "version": "3.11.1",
3
+ "version": "3.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",
package/readme.md CHANGED
@@ -119,6 +119,7 @@ Click the type names for complete docs.
119
119
  - [`UnwrapOpaque`](source/opaque.d.ts) - Revert an [opaque type](https://codemix.com/opaque-types-in-javascript/) back to its original type.
120
120
  - [`InvariantOf`](source/invariant-of.d.ts) - 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.
121
121
  - [`SetOptional`](source/set-optional.d.ts) - Create a type that makes the given keys optional.
122
+ - [`SetReadonly`](source/set-readonly.d.ts) - Create a type that makes the given keys readonly.
122
123
  - [`SetRequired`](source/set-required.d.ts) - Create a type that makes the given keys required.
123
124
  - [`SetNonNullable`](source/set-non-nullable.d.ts) - Create a type that makes the given keys non-nullable.
124
125
  - [`ValueOf`](source/value-of.d.ts) - Create a union of the given object's values, and optionally specify which keys to get the values from.
@@ -143,8 +144,10 @@ Click the type names for complete docs.
143
144
  - [`HasOptionalKeys`](source/has-optional-keys.d.ts) - Create a `true`/`false` type depending on whether the given type has any optional fields.
144
145
  - [`RequiredKeysOf`](source/required-keys-of.d.ts) - Extract all required keys from the given type.
145
146
  - [`HasRequiredKeys`](source/has-required-keys.d.ts) - Create a `true`/`false` type depending on whether the given type has any required fields.
146
- - [`ReadonlyKeysOf`](source/readonly-keys-of.d.ts) - Extract all writable (non-readonly) keys from the given type.
147
- - [`WritableKeysOf`](source/writable-keys-of.d.ts) - Extract all readonly keys from the given type.
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.
148
151
  - [`Spread`](source/spread.d.ts) - Mimic the type inferred by TypeScript when merging two objects or two arrays/tuples using the spread syntax.
149
152
  - [`IsEqual`](source/is-equal.d.ts) - Returns a boolean for whether the two given types are equal.
150
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[]> = Constructor<T, Arguments> & {prototype: T};
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
- export type AbstractClass<T, Arguments extends unknown[] = any[]> = AbstractConstructor<T, Arguments> & {prototype: T};
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;
@@ -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 JsonifyTuple<T extends [unknown, ...unknown[]]> = {
12
- [Key in keyof T]: T[Key] extends NotJsonable ? null : Jsonify<T[Key]>;
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
- ? JsonifyTuple<T>
120
+ ? JsonifyList<T>
111
121
  : T extends ReadonlyArray<infer U>
112
122
  ? Array<U extends NotJsonable ? null : Jsonify<U>>
113
123
  : T extends object
@@ -0,0 +1,38 @@
1
+ import type {Except} from './except';
2
+ import type {Simplify} from './simplify';
3
+
4
+ /**
5
+ Create a type that makes the given keys readonly. The remaining keys are kept as is.
6
+
7
+ Use-case: You want to define a single model where the only thing that changes is whether or not some of the keys are readonly.
8
+
9
+ @example
10
+ ```
11
+ import type {SetReadonly} from 'type-fest';
12
+
13
+ type Foo = {
14
+ a: number;
15
+ readonly b: string;
16
+ c: boolean;
17
+ }
18
+
19
+ type SomeReadonly = SetReadonly<Foo, 'b' | 'c'>;
20
+ // type SomeReadonly = {
21
+ // a: number;
22
+ // readonly b: string; // Was already readonly and still is.
23
+ // readonly c: boolean; // Is now readonly.
24
+ // }
25
+ ```
26
+
27
+ @category Object
28
+ */
29
+ export type SetReadonly<BaseType, Keys extends keyof BaseType> =
30
+ // `extends unknown` is always going to be the case and is used to convert any
31
+ // union into a [distributive conditional
32
+ // type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html#distributive-conditional-types).
33
+ BaseType extends unknown
34
+ ? Simplify<
35
+ Except<BaseType, Keys> &
36
+ Readonly<Pick<BaseType, Keys>>
37
+ >
38
+ : never;