type-fest 3.10.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.10.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,12 +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"
42
- },
43
- "peerDependencies": {
44
- "typescript": ">=4.7.0"
39
+ "tsd": "^0.28.1",
40
+ "typescript": "^5.0.4",
41
+ "xo": "^0.54.2"
45
42
  },
46
43
  "xo": {
47
44
  "rules": {
@@ -50,7 +47,8 @@
50
47
  "@typescript-eslint/naming-convention": "off",
51
48
  "import/extensions": "off",
52
49
  "@typescript-eslint/no-redeclare": "off",
53
- "@typescript-eslint/no-confusing-void-expression": "off"
50
+ "@typescript-eslint/no-confusing-void-expression": "off",
51
+ "@typescript-eslint/no-unsafe-argument": "off"
54
52
  }
55
53
  },
56
54
  "tsd": {
package/readme.md CHANGED
@@ -108,6 +108,7 @@ Click the type names for complete docs.
108
108
  - [`RequireAtLeastOne`](source/require-at-least-one.d.ts) - Create a type that requires at least one of the given keys.
109
109
  - [`RequireExactlyOne`](source/require-exactly-one.d.ts) - Create a type that requires exactly a single key of the given keys and disallows more.
110
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.
111
112
  - [`OmitIndexSignature`](source/omit-index-signature.d.ts) - Omit any index signatures from the given object type, leaving only explicitly defined properties.
112
113
  - [`PickIndexSignature`](source/pick-index-signature.d.ts) - Pick only index signatures from the given object type, leaving out all explicitly defined properties.
113
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.
@@ -142,6 +143,8 @@ Click the type names for complete docs.
142
143
  - [`HasOptionalKeys`](source/has-optional-keys.d.ts) - Create a `true`/`false` type depending on whether the given type has any optional fields.
143
144
  - [`RequiredKeysOf`](source/required-keys-of.d.ts) - Extract all required keys from the given type.
144
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.
145
148
  - [`Spread`](source/spread.d.ts) - Mimic the type inferred by TypeScript when merging two objects or two arrays/tuples using the spread syntax.
146
149
  - [`IsEqual`](source/is-equal.d.ts) - Returns a boolean for whether the two given types are equal.
147
150
  - [`TaggedUnion`](source/tagged-union.d.ts) - Create a union of types that share a common discriminant property.
@@ -239,7 +242,7 @@ type ShouldBeNever = IfAny<'not any', 'not never', 'never'>;
239
242
  - [`PascalCasedProperties`](source/pascal-cased-properties.d.ts) - Converts object properties to pascal-case (`FooBar`)
240
243
  - [`PascalCasedPropertiesDeep`](source/pascal-cased-properties-deep.d.ts) - Converts object properties to pascal-case (`FooBar`)
241
244
  - [`SnakeCase`](source/snake-case.d.ts) - Convert a string literal to snake-case (`foo_bar`).
242
- - [`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`).
243
246
  - [`SnakeCasedPropertiesDeep`](source/snake-cased-properties-deep.d.ts) - Convert object properties to snake-case recursively (`foo_bar`).
244
247
  - [`ScreamingSnakeCase`](source/screaming-snake-case.d.ts) - Convert a string literal to screaming-snake-case (`FOO_BAR`).
245
248
  - [`DelimiterCase`](source/delimiter-case.d.ts) - Convert a string literal to a custom string delimiter casing.
@@ -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
+ };
@@ -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]>;