type-fest 3.9.0 → 3.11.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
@@ -25,6 +25,7 @@ export type {
25
25
  } from './source/omit-index-signature';
26
26
  export type {PickIndexSignature} from './source/pick-index-signature';
27
27
  export type {PartialDeep, PartialDeepOptions} from './source/partial-deep';
28
+ export type {RequiredDeep} from './source/required-deep';
28
29
  export type {PartialOnUndefinedDeep, PartialOnUndefinedDeepOptions} from './source/partial-on-undefined-deep';
29
30
  export type {ReadonlyDeep} from './source/readonly-deep';
30
31
  export type {LiteralUnion} from './source/literal-union';
@@ -76,6 +77,8 @@ export type {OverrideProperties} from './source/override-properties';
76
77
  export type {HasOptionalKeys} from './source/has-optional-keys';
77
78
  export type {RequiredKeysOf} from './source/required-keys-of';
78
79
  export type {HasRequiredKeys} from './source/has-required-keys';
80
+ export type {ReadonlyKeysOf} from './source/readonly-keys-of';
81
+ export type {WritableKeysOf} from './source/writable-keys-of';
79
82
  export type {Spread} from './source/spread';
80
83
  export type {TupleToUnion} from './source/tuple-to-union';
81
84
  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.9.0",
3
+ "version": "3.11.0",
4
4
  "description": "A collection of essential TypeScript types",
5
5
  "license": "(MIT OR CC0-1.0)",
6
6
  "repository": "sindresorhus/type-fest",
@@ -36,9 +36,9 @@
36
36
  "devDependencies": {
37
37
  "@sindresorhus/tsconfig": "~0.7.0",
38
38
  "expect-type": "^0.15.0",
39
- "tsd": "^0.28.0",
40
- "typescript": "^5.0.2",
41
- "xo": "^0.53.1"
39
+ "tsd": "^0.28.1",
40
+ "typescript": "^5.0.4",
41
+ "xo": "^0.54.2"
42
42
  },
43
43
  "xo": {
44
44
  "rules": {
@@ -47,7 +47,8 @@
47
47
  "@typescript-eslint/naming-convention": "off",
48
48
  "import/extensions": "off",
49
49
  "@typescript-eslint/no-redeclare": "off",
50
- "@typescript-eslint/no-confusing-void-expression": "off"
50
+ "@typescript-eslint/no-confusing-void-expression": "off",
51
+ "@typescript-eslint/no-unsafe-argument": "off"
51
52
  }
52
53
  },
53
54
  "tsd": {
package/readme.md CHANGED
@@ -37,37 +37,6 @@
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://www.useanvil.com/?utm_source=sindresorhus#gh-light-mode-only">
44
- <div>
45
- <img src="https://sindresorhus.com/assets/thanks/anvil-logo-light.svg" width="200" alt="Anvil">
46
- </div>
47
- <br>
48
- <b>Paperwork that makes the data work.</b>
49
- <div>
50
- <sub>
51
- Easy APIs for paperwork. PDF generation, e-signature and embeddable no-code webforms.
52
- <br>
53
- The easiest way to build paperwork automation into your product.
54
- </sub>
55
- </div>
56
- </a>
57
- <a href="https://www.useanvil.com/?utm_source=sindresorhus#gh-dark-mode-only">
58
- <div>
59
- <img src="https://sindresorhus.com/assets/thanks/anvil-logo-dark.svg" width="200" alt="Anvil">
60
- </div>
61
- <br>
62
- <b>Paperwork that makes the data work.</b>
63
- <div>
64
- <sub>
65
- Easy APIs for paperwork. PDF generation, e-signature and embeddable no-code webforms.
66
- <br>
67
- The easiest way to build paperwork automation into your product.
68
- </sub>
69
- </div>
70
- </a>
71
40
  </p>
72
41
  </div>
73
42
  <br>
@@ -139,6 +108,7 @@ Click the type names for complete docs.
139
108
  - [`RequireAtLeastOne`](source/require-at-least-one.d.ts) - Create a type that requires at least one of the given keys.
140
109
  - [`RequireExactlyOne`](source/require-exactly-one.d.ts) - Create a type that requires exactly a single key of the given keys and disallows more.
141
110
  - [`RequireAllOrNone`](source/require-all-or-none.d.ts) - Create a type that requires all of the given keys or none of the given keys.
111
+ - [`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.
142
112
  - [`OmitIndexSignature`](source/omit-index-signature.d.ts) - Omit any index signatures from the given object type, leaving only explicitly defined properties.
143
113
  - [`PickIndexSignature`](source/pick-index-signature.d.ts) - Pick only index signatures from the given object type, leaving out all explicitly defined properties.
144
114
  - [`PartialDeep`](source/partial-deep.d.ts) - Create a deeply optional version of another type. Use [`Partial<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype) if you only need one level deep.
@@ -173,6 +143,8 @@ Click the type names for complete docs.
173
143
  - [`HasOptionalKeys`](source/has-optional-keys.d.ts) - Create a `true`/`false` type depending on whether the given type has any optional fields.
174
144
  - [`RequiredKeysOf`](source/required-keys-of.d.ts) - Extract all required keys from the given type.
175
145
  - [`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.
176
148
  - [`Spread`](source/spread.d.ts) - Mimic the type inferred by TypeScript when merging two objects or two arrays/tuples using the spread syntax.
177
149
  - [`IsEqual`](source/is-equal.d.ts) - Returns a boolean for whether the two given types are equal.
178
150
  - [`TaggedUnion`](source/tagged-union.d.ts) - Create a union of types that share a common discriminant property.
@@ -270,7 +242,7 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
270
242
  - [`PascalCasedProperties`](source/pascal-cased-properties.d.ts) - Converts object properties to pascal-case (`FooBar`)
271
243
  - [`PascalCasedPropertiesDeep`](source/pascal-cased-properties-deep.d.ts) - Converts object properties to pascal-case (`FooBar`)
272
244
  - [`SnakeCase`](source/snake-case.d.ts) - Convert a string literal to snake-case (`foo_bar`).
273
- - [`SnakeCasedProperties`](source/snake-cased-properties-deep.d.ts) - Convert object properties to snake-case (`foo_bar`).
245
+ - [`SnakeCasedProperties`](source/snake-cased-properties.d.ts) - Convert object properties to snake-case (`foo_bar`).
274
246
  - [`SnakeCasedPropertiesDeep`](source/snake-cased-properties-deep.d.ts) - Convert object properties to snake-case recursively (`foo_bar`).
275
247
  - [`ScreamingSnakeCase`](source/screaming-snake-case.d.ts) - Convert a string literal to screaming-snake-case (`FOO_BAR`).
276
248
  - [`DelimiterCase`](source/delimiter-case.d.ts) - Convert a string literal to a custom string delimiter casing.
@@ -15,12 +15,15 @@ type Bar = OverrideProperties<Foo, {b: number}>
15
15
  //=> {a: string, b: number}
16
16
 
17
17
  type Baz = OverrideProperties<Foo, {c: number}>
18
- // error TS2559: Type '{c: number}' has no properties in common with type 'Partial{a: unknown; b: unknown}>'.
18
+ // Error, type '{ c: number; }' does not satisfy the constraint '{ c: never; }'
19
+
20
+ type Fizz = OverrideProperties<Foo, {b: number; c: number}>
21
+ // Error, type '{ b: number; c: number; }' does not satisfy the constraint '{ b: number; c: never; }'
19
22
  ```
20
23
 
21
24
  @category Object
22
25
  */
23
26
  export type OverrideProperties<
24
27
  TOriginal,
25
- TOverride extends Partial<{[key in keyof TOriginal]: unknown}>,
28
+ TOverride extends {[Key in keyof TOverride]: Key extends keyof TOriginal ? TOverride[Key] : never},
26
29
  > = Merge<TOriginal, TOverride>;
@@ -0,0 +1,29 @@
1
+ import type {IsEqual} from './is-equal';
2
+
3
+ /**
4
+ Extract all readonly keys from the given type.
5
+
6
+ This is useful when you want to create a new type that contains readonly keys only.
7
+
8
+ @example
9
+ ```
10
+ import type {ReadonlyKeysOf} from 'type-fest';
11
+
12
+ interface User {
13
+ name: string;
14
+ surname: string;
15
+ readonly id: number;
16
+ }
17
+
18
+ type UpdateResponse<Entity extends object> = Pick<Entity, ReadonlyKeysOf<Entity>>;
19
+
20
+ const update1: UpdateResponse<User> = {
21
+ id: 123,
22
+ };
23
+ ```
24
+
25
+ @category Utilities
26
+ */
27
+ export type ReadonlyKeysOf<T> = NonNullable<{
28
+ [P in keyof T]: IsEqual<{[Q in P]: T[P]}, {readonly [Q in P]: T[P]}> extends true ? P : never
29
+ }[keyof T]>;
@@ -0,0 +1,78 @@
1
+ import type {BuiltIns, HasMultipleCallSignatures} from './internal';
2
+
3
+ type ExcludeUndefined<T> = Exclude<T, undefined>;
4
+
5
+ /**
6
+ Create a type from another type with all keys and nested keys set to required.
7
+
8
+ Use-cases:
9
+ - Creating optional configuration interfaces where the underlying implementation still requires all options to be fully specified.
10
+ - Modeling the resulting type after a deep merge with a set of defaults.
11
+
12
+ @example
13
+ ```
14
+ import type {RequiredDeep} from 'type-fest';
15
+
16
+ type Settings = {
17
+ textEditor?: {
18
+ fontSize?: number | undefined;
19
+ fontColor?: string | undefined;
20
+ fontWeight?: number | undefined;
21
+ }
22
+ autocomplete?: boolean | undefined;
23
+ autosave?: boolean | undefined;
24
+ };
25
+
26
+ type RequiredSettings = RequiredDeep<Settings>;
27
+ // type RequiredSettings = {
28
+ // textEditor: {
29
+ // fontSize: number;
30
+ // fontColor: string;
31
+ // fontWeight: number;
32
+ // }
33
+ // autocomplete: boolean;
34
+ // autosave: boolean;
35
+ // }
36
+ ```
37
+
38
+ Note that types containing overloaded functions are not made deeply required due to a [TypeScript limitation](https://github.com/microsoft/TypeScript/issues/29732).
39
+
40
+ @category Utilities
41
+ @category Object
42
+ @category Array
43
+ @category Set
44
+ @category Map
45
+ */
46
+ export type RequiredDeep<T, E extends ExcludeUndefined<T> = ExcludeUndefined<T>> = E extends BuiltIns
47
+ ? E
48
+ : E extends Map<infer KeyType, infer ValueType>
49
+ ? Map<RequiredDeep<KeyType>, RequiredDeep<ValueType>>
50
+ : E extends Set<infer ItemType>
51
+ ? Set<RequiredDeep<ItemType>>
52
+ : E extends ReadonlyMap<infer KeyType, infer ValueType>
53
+ ? ReadonlyMap<RequiredDeep<KeyType>, RequiredDeep<ValueType>>
54
+ : E extends ReadonlySet<infer ItemType>
55
+ ? ReadonlySet<RequiredDeep<ItemType>>
56
+ : E extends WeakMap<infer KeyType, infer ValueType>
57
+ ? WeakMap<RequiredDeep<KeyType>, RequiredDeep<ValueType>>
58
+ : E extends WeakSet<infer ItemType>
59
+ ? WeakSet<RequiredDeep<ItemType>>
60
+ : E extends Promise<infer ValueType>
61
+ ? Promise<RequiredDeep<ValueType>>
62
+ : E extends (...args: any[]) => unknown
63
+ ? {} extends RequiredObjectDeep<E>
64
+ ? E
65
+ : HasMultipleCallSignatures<E> extends true
66
+ ? E
67
+ : ((...arguments: Parameters<E>) => ReturnType<E>) & RequiredObjectDeep<E>
68
+ : E extends object
69
+ ? E extends Array<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
70
+ ? ItemType[] extends E // Test for arrays (non-tuples) specifically
71
+ ? Array<RequiredDeep<ItemType>> // Recreate relevant array type to prevent eager evaluation of circular reference
72
+ : RequiredObjectDeep<E> // Tuples behave properly
73
+ : RequiredObjectDeep<E>
74
+ : unknown;
75
+
76
+ type RequiredObjectDeep<ObjectType extends object> = {
77
+ [KeyType in keyof ObjectType]-?: RequiredDeep<ObjectType[KeyType]>
78
+ };
@@ -28,8 +28,7 @@ type SomeRequired = SetRequired<Foo, 'b' | 'c'>;
28
28
  */
29
29
  export type SetRequired<BaseType, Keys extends keyof BaseType> =
30
30
  Simplify<
31
- // Pick just the keys that are optional from the base type.
32
- Except<BaseType, Keys> &
31
+ BaseType &
33
32
  // Pick the keys that should be required from the base type and make them required.
34
33
  Required<Pick<BaseType, Keys>>
35
34
  >;
@@ -0,0 +1,30 @@
1
+ import type {IsEqual} from './is-equal';
2
+
3
+ /**
4
+ Extract all writable keys from the given type.
5
+
6
+ This is useful when you want to create a new type that contains writable keys only.
7
+
8
+ @example
9
+ ```
10
+ import type {WritableKeysOf} from 'type-fest';
11
+
12
+ interface User {
13
+ name: string;
14
+ surname: string;
15
+ readonly id: number;
16
+ }
17
+
18
+ type UpdateRequest<Entity extends object> = Pick<Entity, WritableKeysOf<Entity>>;
19
+
20
+ const update1: UpdateRequest<User> = {
21
+ name: 'Alice',
22
+ surname: 'Acme',
23
+ };
24
+ ```
25
+
26
+ @category Utilities
27
+ */
28
+ export type WritableKeysOf<T> = NonNullable<{
29
+ [P in keyof T]: IsEqual<{[Q in P]: T[P]}, {readonly [Q in P]: T[P]}> extends false ? P : never
30
+ }[keyof T]>;