type-fest 2.18.1 → 2.19.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
@@ -23,6 +23,7 @@ export {Opaque, UnwrapOpaque} from './source/opaque';
23
23
  export {InvariantOf} from './source/invariant-of';
24
24
  export {SetOptional} from './source/set-optional';
25
25
  export {SetRequired} from './source/set-required';
26
+ export {SetNonNullable} from './source/set-non-nullable';
26
27
  export {ValueOf} from './source/value-of';
27
28
  export {PromiseValue} from './source/promise-value';
28
29
  export {AsyncReturnType} from './source/async-return-type';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-fest",
3
- "version": "2.18.1",
3
+ "version": "2.19.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
@@ -142,6 +142,7 @@ Click the type names for complete docs.
142
142
  - [`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.
143
143
  - [`SetOptional`](source/set-optional.d.ts) - Create a type that makes the given keys optional.
144
144
  - [`SetRequired`](source/set-required.d.ts) - Create a type that makes the given keys required.
145
+ - [`SetNonNullable`](source/set-non-nullable.d.ts) - Create a type that makes the given keys non-nullable.
145
146
  - [`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.
146
147
  - [`ConditionalKeys`](source/conditional-keys.d.ts) - Extract keys from a shape where values extend the given `Condition` type.
147
148
  - [`ConditionalPick`](source/conditional-pick.d.ts) - Like `Pick` except it selects properties from a shape where the values extend the given `Condition` type.
package/source/exact.d.ts CHANGED
@@ -1,6 +1,29 @@
1
- import type {Primitive} from './primitive';
2
1
  import type {KeysOfUnion} from './internal';
3
2
 
3
+ /**
4
+ Extract the element of an array that also works for array union.
5
+
6
+ Returns `never` if T is not an array.
7
+
8
+ It creates a type-safe way to access the element type of `unknown` type.
9
+ */
10
+ type ArrayElement<T> = T extends readonly unknown[] ? T[0] : never;
11
+
12
+ /**
13
+ Extract the object field type if T is an object and K is a key of T, return `never` otherwise.
14
+
15
+ It creates a type-safe way to access the member type of `unknown` type.
16
+ */
17
+ type ObjectValue<T, K> = K extends keyof T ? T[K] : never;
18
+
19
+ /**
20
+ Create a type from `ParameterType` and `InputType` and change keys exclusive to `InputType` to `never`.
21
+ - Generate a list of keys that exists in `InputType` but not in `ParameterType`.
22
+ - Mark these excess keys as `never`.
23
+ */
24
+ type ExactObject<ParameterType, InputType> = {[Key in keyof ParameterType]: Exact<ParameterType[Key], ObjectValue<InputType, Key>>}
25
+ & Record<Exclude<keyof InputType, KeysOfUnion<ParameterType>>, never>;
26
+
4
27
  /**
5
28
  Create a type that does not allow extra properties, meaning it only allows properties that are explicitly declared.
6
29
 
@@ -41,11 +64,10 @@ onlyAcceptNameImproved(invalidInput); // Compilation error
41
64
 
42
65
  @category Utilities
43
66
  */
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>;
67
+ export type Exact<ParameterType, InputType> =
68
+ // Convert union of array to array of union: A[] & B[] => (A & B)[]
69
+ ParameterType extends unknown[] ? Array<Exact<ArrayElement<ParameterType>, ArrayElement<InputType>>>
70
+ // In TypeScript, Array is a subtype of ReadonlyArray, so always test Array before ReadonlyArray.
71
+ : ParameterType extends readonly unknown[] ? ReadonlyArray<Exact<ArrayElement<ParameterType>, ArrayElement<InputType>>>
72
+ : ParameterType extends object ? ExactObject<ParameterType, InputType>
73
+ : ParameterType;
@@ -223,15 +223,17 @@ declare namespace PackageJson {
223
223
  string
224
224
  >;
225
225
 
226
+ type ExportConditions = {[condition in ExportCondition]: Exports};
227
+
226
228
  /**
227
229
  Entry points of a module, optionally with conditions and subpath exports.
228
230
  */
229
231
  export type Exports =
230
232
  | null
231
233
  | string
232
- | string[]
233
- | {[key in ExportCondition]: Exports}
234
- | {[key: string]: Exports}; // eslint-disable-line @typescript-eslint/consistent-indexed-object-style
234
+ | Array<string | ExportConditions>
235
+ | ExportConditions
236
+ | {[path: string]: Exports}; // eslint-disable-line @typescript-eslint/consistent-indexed-object-style
235
237
 
236
238
  /**
237
239
  Import map entries of a module, optionally with conditions.
@@ -0,0 +1,35 @@
1
+ import type {Except} from './except';
2
+ import type {Simplify} from './simplify';
3
+
4
+ /**
5
+ Create a type that makes the given keys non-nullable. 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 non-nullable.
8
+
9
+ @example
10
+ ```
11
+ import type {SetNonNullable} from 'type-fest';
12
+
13
+ type Foo = {
14
+ a: number;
15
+ b: string | undefined;
16
+ c?: boolean | null;
17
+ }
18
+
19
+ type SomeNonNullable = SetNonNullable<Foo, 'b' | 'c'>;
20
+ // type SomeNonNullable = {
21
+ // a: number;
22
+ // b: string; // Can no longer be undefined.
23
+ // c?: boolean; // Can no longer be null, but is still optional.
24
+ // }
25
+ ```
26
+
27
+ @category Object
28
+ */
29
+ export type SetNonNullable<BaseType, Keys extends keyof BaseType> =
30
+ Simplify<
31
+ // Pick just the keys that are readonly from the base type.
32
+ Except<BaseType, Keys> &
33
+ // Pick the keys that should be non-nullable from the base type and make them non-nullable.
34
+ {[Key in Keys]: NonNullable<BaseType[Key]>}
35
+ >;