type-fest 2.6.0 → 2.10.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.
Files changed (68) hide show
  1. package/index.d.ts +15 -0
  2. package/package.json +1 -1
  3. package/readme.md +66 -36
  4. package/source/async-return-type.d.ts +1 -1
  5. package/source/asyncify.d.ts +1 -1
  6. package/source/basic.d.ts +6 -6
  7. package/source/camel-case.d.ts +2 -1
  8. package/source/camel-cased-properties-deep.d.ts +3 -1
  9. package/source/camel-cased-properties.d.ts +3 -1
  10. package/source/conditional-except.d.ts +1 -1
  11. package/source/conditional-keys.d.ts +1 -1
  12. package/source/conditional-pick.d.ts +1 -1
  13. package/source/delimiter-case.d.ts +3 -2
  14. package/source/delimiter-cased-properties-deep.d.ts +4 -2
  15. package/source/delimiter-cased-properties.d.ts +3 -1
  16. package/source/entries.d.ts +4 -1
  17. package/source/entry.d.ts +4 -1
  18. package/source/except.d.ts +1 -1
  19. package/source/fixed-length-array.d.ts +1 -1
  20. package/source/get.d.ts +56 -12
  21. package/source/includes.d.ts +1 -1
  22. package/source/internal.d.ts +7 -0
  23. package/source/iterable-element.d.ts +1 -1
  24. package/source/join.d.ts +2 -1
  25. package/source/jsonify.d.ts +2 -2
  26. package/source/kebab-case.d.ts +2 -1
  27. package/source/kebab-cased-properties-deep.d.ts +3 -1
  28. package/source/kebab-cased-properties.d.ts +3 -1
  29. package/source/last-array-element.d.ts +2 -1
  30. package/source/literal-to-primitive.d.ts +36 -0
  31. package/source/literal-union.d.ts +3 -3
  32. package/source/merge-exclusive.d.ts +1 -1
  33. package/source/merge.d.ts +1 -1
  34. package/source/multidimensional-array.d.ts +1 -1
  35. package/source/multidimensional-readonly-array.d.ts +1 -1
  36. package/source/mutable.d.ts +1 -1
  37. package/source/numeric.d.ts +168 -0
  38. package/source/observable-like.d.ts +15 -1
  39. package/source/opaque.d.ts +1 -1
  40. package/source/package-json.d.ts +1 -1
  41. package/source/partial-deep.d.ts +7 -4
  42. package/source/pascal-case.d.ts +2 -1
  43. package/source/pascal-cased-properties-deep.d.ts +4 -2
  44. package/source/pascal-cased-properties.d.ts +3 -1
  45. package/source/primitive.d.ts +1 -1
  46. package/source/promisable.d.ts +2 -2
  47. package/source/promise-value.d.ts +1 -1
  48. package/source/readonly-deep.d.ts +6 -3
  49. package/source/remove-index-signature.d.ts +98 -0
  50. package/source/require-all-or-none.d.ts +1 -1
  51. package/source/require-at-least-one.d.ts +1 -1
  52. package/source/require-exactly-one.d.ts +1 -1
  53. package/source/screaming-snake-case.d.ts +2 -1
  54. package/source/set-optional.d.ts +1 -1
  55. package/source/set-required.d.ts +1 -1
  56. package/source/set-return-type.d.ts +1 -1
  57. package/source/simplify.d.ts +1 -1
  58. package/source/snake-case.d.ts +2 -1
  59. package/source/snake-cased-properties-deep.d.ts +3 -1
  60. package/source/snake-cased-properties.d.ts +3 -1
  61. package/source/split.d.ts +2 -1
  62. package/source/string-key-of.d.ts +25 -0
  63. package/source/stringified.d.ts +1 -1
  64. package/source/trim.d.ts +2 -1
  65. package/source/tsconfig-json.d.ts +1 -1
  66. package/source/typed-array.d.ts +1 -1
  67. package/source/union-to-intersection.d.ts +1 -1
  68. package/source/value-of.d.ts +1 -1
package/index.d.ts CHANGED
@@ -12,6 +12,7 @@ export {MergeExclusive} from './source/merge-exclusive';
12
12
  export {RequireAtLeastOne} from './source/require-at-least-one';
13
13
  export {RequireExactlyOne} from './source/require-exactly-one';
14
14
  export {RequireAllOrNone} from './source/require-all-or-none';
15
+ export {RemoveIndexSignature} from './source/remove-index-signature';
15
16
  export {PartialDeep} from './source/partial-deep';
16
17
  export {ReadonlyDeep} from './source/readonly-deep';
17
18
  export {LiteralUnion} from './source/literal-union';
@@ -37,6 +38,20 @@ export {SetReturnType} from './source/set-return-type';
37
38
  export {Asyncify} from './source/asyncify';
38
39
  export {Simplify} from './source/simplify';
39
40
  export {Jsonify} from './source/jsonify';
41
+ export {LiteralToPrimitive} from './source/literal-to-primitive';
42
+ export {
43
+ PositiveInfinity,
44
+ NegativeInfinity,
45
+ Finite,
46
+ Integer,
47
+ Float,
48
+ NegativeFloat,
49
+ Negative,
50
+ NonNegative,
51
+ NegativeInteger,
52
+ NonNegativeInteger,
53
+ } from './source/numeric';
54
+ export {StringKeyOf} from './source/string-key-of';
40
55
 
41
56
  // Template literal types
42
57
  export {CamelCase} from './source/camel-case';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-fest",
3
- "version": "2.6.0",
3
+ "version": "2.10.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
@@ -88,10 +88,6 @@ Click the type names for complete docs.
88
88
  - [`Class`](source/basic.d.ts) - Matches a [`class`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
89
89
  - [`Constructor`](source/basic.d.ts) - Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
90
90
  - [`TypedArray`](source/typed-array.d.ts) - Matches any [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray), like `Uint8Array` or `Float64Array`.
91
- - [`JsonPrimitive`](source/basic.d.ts) - Matches a JSON primitive.
92
- - [`JsonObject`](source/basic.d.ts) - Matches a JSON object.
93
- - [`JsonArray`](source/basic.d.ts) - Matches a JSON array.
94
- - [`JsonValue`](source/basic.d.ts) - Matches any valid JSON value.
95
91
  - [`ObservableLike`](source/observable-like.d.ts) - Matches a value that is like an [Observable](https://github.com/tc39/proposal-observable).
96
92
 
97
93
  ### Utilities
@@ -103,55 +99,87 @@ Click the type names for complete docs.
103
99
  - [`RequireAtLeastOne`](source/require-at-least-one.d.ts) - Create a type that requires at least one of the given keys.
104
100
  - [`RequireExactlyOne`](source/require-exactly-one.d.ts) - Create a type that requires exactly a single key of the given keys and disallows more.
105
101
  - [`RequireAllOrNone`](source/require-all-or-none.d.ts) - Create a type that requires all of the given keys or none of the given keys.
102
+ - [`RemoveIndexSignature`](source/remove-index-signature.d.ts) - Create a type that only has explicitly defined properties, absent of any index signatures.
106
103
  - [`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.
107
104
  - [`ReadonlyDeep`](source/readonly-deep.d.ts) - Create a deeply immutable version of an `object`/`Map`/`Set`/`Array` type. Use [`Readonly<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#readonlytype) if you only need one level deep.
108
105
  - [`LiteralUnion`](source/literal-union.d.ts) - Create a union type by combining primitive types and literal types without sacrificing auto-completion in IDEs for the literal type part of the union. Workaround for [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729).
109
- - [`Promisable`](source/promisable.d.ts) - Create a type that represents either the value or the value wrapped in `PromiseLike`.
110
106
  - [`Opaque`](source/opaque.d.ts) - Create an [opaque type](https://codemix.com/opaque-types-in-javascript/).
111
107
  - [`SetOptional`](source/set-optional.d.ts) - Create a type that makes the given keys optional.
112
108
  - [`SetRequired`](source/set-required.d.ts) - Create a type that makes the given keys required.
113
109
  - [`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.
114
- - [`AsyncReturnType`](source/async-return-type.d.ts) - Unwrap the return type of a function that returns a `Promise`.
115
110
  - [`ConditionalKeys`](source/conditional-keys.d.ts) - Extract keys from a shape where values extend the given `Condition` type.
116
111
  - [`ConditionalPick`](source/conditional-pick.d.ts) - Like `Pick` except it selects properties from a shape where the values extend the given `Condition` type.
117
112
  - [`ConditionalExcept`](source/conditional-except.d.ts) - Like `Omit` except it removes properties from a shape where the values extend the given `Condition` type.
118
113
  - [`UnionToIntersection`](source/union-to-intersection.d.ts) - Convert a union type to an intersection type.
114
+ - [`LiteralToPrimitive`](source/literal-to-primitive.d.ts) - Convert a [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types) to the [primitive type](source/primitive.d.ts) it belongs to.
119
115
  - [`Stringified`](source/stringified.d.ts) - Create a type with the keys of the given type changed to `string` type.
120
- - [`FixedLengthArray`](source/fixed-length-array.d.ts) - Create a type that represents an array of the given type and length.
121
- - [`MultidimensionalArray`](source/multidimensional-array.d.ts) - Create a type that represents a multidimensional array of the given type and dimensions.
122
- - [`MultidimensionalReadonlyArray`](source/multidimensional-readonly-array.d.ts) - Create a type that represents a multidimensional readonly array of the given type and dimensions.
123
116
  - [`IterableElement`](source/iterable-element.d.ts) - Get the element type of an `Iterable`/`AsyncIterable`. For example, an array or a generator.
124
117
  - [`Entry`](source/entry.d.ts) - Create a type that represents the type of an entry of a collection.
125
118
  - [`Entries`](source/entries.d.ts) - Create a type that represents the type of the entries of a collection.
126
119
  - [`SetReturnType`](source/set-return-type.d.ts) - Create a function type with a return type of your choice and the same parameters as the given function type.
127
- - [`Asyncify`](source/asyncify.d.ts) - Create an async version of the given function type.
128
- - [`Includes`](source/includes.d.ts) - Returns a boolean for whether the given array includes the given item.
129
120
  - [`Simplify`](source/simplify.d.ts) - Useful to flatten the type output to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.
121
+ - [`Get`](source/get.d.ts) - Get a deeply-nested property from an object using a key path, like [Lodash's `.get()`](https://lodash.com/docs/latest#get) function.
122
+ - [`StringKeyOf`](source/string-key-of.d.ts) - Get keys of the given type as strings.
123
+
124
+ ### JSON
125
+
130
126
  - [`Jsonify`](source/jsonify.d.ts) - Transform a type to one that is assignable to the `JsonValue` type.
127
+ - [`JsonPrimitive`](source/basic.d.ts) - Matches a JSON primitive.
128
+ - [`JsonObject`](source/basic.d.ts) - Matches a JSON object.
129
+ - [`JsonArray`](source/basic.d.ts) - Matches a JSON array.
130
+ - [`JsonValue`](source/basic.d.ts) - Matches any valid JSON value.
131
+
132
+ ### Async
133
+
134
+ - [`Promisable`](source/promisable.d.ts) - Create a type that represents either the value or the value wrapped in `PromiseLike`.
135
+ - [`AsyncReturnType`](source/async-return-type.d.ts) - Unwrap the return type of a function that returns a `Promise`.
136
+ - [`Asyncify`](source/asyncify.d.ts) - Create an async version of the given function type.
137
+
138
+ ### String
131
139
 
132
- ### Template literal types
133
-
134
- - [`CamelCase`](source/camel-case.d.ts) – Convert a string literal to camel-case (`fooBar`).
135
- - [`CamelCasedProperties`](source/camel-cased-properties.d.ts) – Convert object properties to camel-case (`fooBar`).
136
- - [`CamelCasedPropertiesDeep`](source/camel-cased-properties-deep.d.ts) – Convert object properties to camel-case recursively (`fooBar`).
137
- - [`KebabCase`](source/kebab-case.d.ts) – Convert a string literal to kebab-case (`foo-bar`).
138
- - [`KebabCasedProperties`](source/kebab-cased-properties.d.ts) – Convert a object properties to kebab-case recursively (`foo-bar`).
139
- - [`KebabCasedPropertiesDeep`](source/kebab-cased-properties-deep.d.ts) – Convert object properties to kebab-case (`foo-bar`).
140
- - [`PascalCase`](source/pascal-case.d.ts) – Converts a string literal to pascal-case (`FooBar`)
141
- - [`PascalCasedProperties`](source/pascal-cased-properties.d.ts) – Converts object properties to pascal-case (`FooBar`)
142
- - [`PascalCasedPropertiesDeep`](source/pascal-cased-properties-deep.d.ts) – Converts object properties to pascal-case (`FooBar`)
143
- - [`SnakeCase`](source/snake-case.d.ts) – Convert a string literal to snake-case (`foo_bar`).
144
- - [`SnakeCasedProperties`](source/snake-cased-properties-deep.d.ts) – Convert object properties to snake-case (`foo_bar`).
145
- - [`SnakeCasedPropertiesDeep`](source/snake-cased-properties-deep.d.ts) – Convert object properties to snake-case recursively (`foo_bar`).
146
- - [`ScreamingSnakeCase`](source/screaming-snake-case.d.ts) - Convert a string literal to screaming-snake-case (`FOO_BAR`).
147
- - [`DelimiterCase`](source/delimiter-case.d.ts) – Convert a string literal to a custom string delimiter casing.
148
- - [`DelimiterCasedProperties`](source/delimiter-cased-properties.d.ts) – Convert object properties to a custom string delimiter casing.
149
- - [`DelimiterCasedPropertiesDeep`](source/delimiter-cased-properties-deep.d.ts) – Convert object properties to a custom string delimiter casing recursively.
150
- - [`Join`](source/join.d.ts) - Join an array of strings and/or numbers using the given string as a delimiter.
151
- - [`Split`](source/split.d.ts) - Represents an array of strings split using a given character or character set.
152
140
  - [`Trim`](source/trim.d.ts) - Remove leading and trailing spaces from a string.
153
- - [`Get`](source/get.d.ts) - Get a deeply-nested property from an object using a key path, like [Lodash's `.get()`](https://lodash.com/docs/latest#get) function.
141
+ - [`Split`](source/split.d.ts) - Represents an array of strings split using a given character or character set.
142
+
143
+ ### Array
144
+
145
+ - [`Includes`](source/includes.d.ts) - Returns a boolean for whether the given array includes the given item.
146
+ - [`Join`](source/join.d.ts) - Join an array of strings and/or numbers using the given string as a delimiter.
154
147
  - [`LastArrayElement`](source/last-array-element.d.ts) - Extracts the type of the last element of an array.
148
+ - [`FixedLengthArray`](source/fixed-length-array.d.ts) - Create a type that represents an array of the given type and length.
149
+ - [`MultidimensionalArray`](source/multidimensional-array.d.ts) - Create a type that represents a multidimensional array of the given type and dimensions.
150
+ - [`MultidimensionalReadonlyArray`](source/multidimensional-readonly-array.d.ts) - Create a type that represents a multidimensional readonly array of the given type and dimensions.
151
+
152
+ ### Numeric
153
+
154
+ - [`PositiveInfinity`](source/numeric.d.ts) - Matches the hidden `Infinity` type.
155
+ - [`NegativeInfinity`](source/numeric.d.ts) - Matches the hidden `-Infinity` type.
156
+ - [`Finite`](source/numeric.d.ts) - A finite `number`.
157
+ - [`Integer`](source/numeric.d.ts) - A `number` that is an integer.
158
+ - [`Float`](source/numeric.d.ts) - A `number` that is not an integer.
159
+ - [`NegativeFloat`](source/numeric.d.ts) - A negative (`-∞ < x < 0`) `number` that is not an integer.
160
+ - [`Negative`](source/numeric.d.ts) - A negative `number`/`bigint` (`-∞ < x < 0`)
161
+ - [`NonNegative`](source/numeric.d.ts) - A non-negative `number`/`bigint` (`0 <= x < ∞`).
162
+ - [`NegativeInteger`](source/numeric.d.ts) - A negative (`-∞ < x < 0`) `number` that is an integer.
163
+ - [`NonNegativeInteger`](source/numeric.d.ts) - A non-negative (`0 <= x < ∞`) `number` that is an integer.
164
+
165
+ ### Change case
166
+
167
+ - [`CamelCase`](source/camel-case.d.ts) - Convert a string literal to camel-case (`fooBar`).
168
+ - [`CamelCasedProperties`](source/camel-cased-properties.d.ts) - Convert object properties to camel-case (`fooBar`).
169
+ - [`CamelCasedPropertiesDeep`](source/camel-cased-properties-deep.d.ts) - Convert object properties to camel-case recursively (`fooBar`).
170
+ - [`KebabCase`](source/kebab-case.d.ts) - Convert a string literal to kebab-case (`foo-bar`).
171
+ - [`KebabCasedProperties`](source/kebab-cased-properties.d.ts) - Convert a object properties to kebab-case recursively (`foo-bar`).
172
+ - [`KebabCasedPropertiesDeep`](source/kebab-cased-properties-deep.d.ts) - Convert object properties to kebab-case (`foo-bar`).
173
+ - [`PascalCase`](source/pascal-case.d.ts) - Converts a string literal to pascal-case (`FooBar`)
174
+ - [`PascalCasedProperties`](source/pascal-cased-properties.d.ts) - Converts object properties to pascal-case (`FooBar`)
175
+ - [`PascalCasedPropertiesDeep`](source/pascal-cased-properties-deep.d.ts) - Converts object properties to pascal-case (`FooBar`)
176
+ - [`SnakeCase`](source/snake-case.d.ts) - Convert a string literal to snake-case (`foo_bar`).
177
+ - [`SnakeCasedProperties`](source/snake-cased-properties-deep.d.ts) - Convert object properties to snake-case (`foo_bar`).
178
+ - [`SnakeCasedPropertiesDeep`](source/snake-cased-properties-deep.d.ts) - Convert object properties to snake-case recursively (`foo_bar`).
179
+ - [`ScreamingSnakeCase`](source/screaming-snake-case.d.ts) - Convert a string literal to screaming-snake-case (`FOO_BAR`).
180
+ - [`DelimiterCase`](source/delimiter-case.d.ts) - Convert a string literal to a custom string delimiter casing.
181
+ - [`DelimiterCasedProperties`](source/delimiter-cased-properties.d.ts) - Convert object properties to a custom string delimiter casing.
182
+ - [`DelimiterCasedPropertiesDeep`](source/delimiter-cased-properties-deep.d.ts) - Convert object properties to a custom string delimiter casing recursively.
155
183
 
156
184
  ### Miscellaneous
157
185
 
@@ -162,10 +190,12 @@ Click the type names for complete docs.
162
190
 
163
191
  *If we decline a type addition, we will make sure to document the better solution here.*
164
192
 
165
- - [`Diff` and `Spread`](https://github.com/sindresorhus/type-fest/pull/7) - The PR author didn't provide any real-world use-cases and the PR went stale. If you think this type is useful, provide some real-world use-cases and we might reconsider.
193
+ - [`Diff` and `Spread`](https://github.com/sindresorhus/type-fest/pull/7) - The pull request author didn't provide any real-world use-cases and the PR went stale. If you think this type is useful, provide some real-world use-cases and we might reconsider.
166
194
  - [`Dictionary`](https://github.com/sindresorhus/type-fest/issues/33) - You only save a few characters (`Dictionary<number>` vs `Record<string, number>`) from [`Record`](https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type), which is more flexible and well-known. Also, you shouldn't use an object as a dictionary. We have `Map` in JavaScript now.
167
195
  - [`ExtractProperties` and `ExtractMethods`](https://github.com/sindresorhus/type-fest/pull/4) - The types violate the single responsibility principle. Instead, refine your types into more granular type hierarchies.
168
196
  - [`Url2Json`](https://github.com/sindresorhus/type-fest/pull/262) - Inferring search parameters from a URL string is a cute idea, but not very useful in practice, since search parameters are usually dynamic and defined separately.
197
+ - [`Nullish`](https://github.com/sindresorhus/type-fest/pull/318) - The type only saves a couple of characters, not everyone knows what "nullish" means, and I'm also trying to [get away from `null`](https://github.com/sindresorhus/meta/discussions/7).
198
+ - [`TitleCase`](https://github.com/sindresorhus/type-fest/pull/303) - It's not solving a common need and is a better fit for a separate package.
169
199
 
170
200
  ## Alternative type names
171
201
 
@@ -580,7 +610,7 @@ There are many advanced types most users don't know about.
580
610
  ```
581
611
  </details>
582
612
 
583
- - [`ReturnType<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#returntypetype) Obtain the return type of a function type.
613
+ - [`ReturnType<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#returntypetype) - Obtain the return type of a function type.
584
614
  <details>
585
615
  <summary>
586
616
  Example
@@ -615,7 +645,7 @@ There are many advanced types most users don't know about.
615
645
  ```
616
646
  </details>
617
647
 
618
- - [`InstanceType<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#instancetypetype) Obtain the instance type of a constructor function type.
648
+ - [`InstanceType<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#instancetypetype) - Obtain the instance type of a constructor function type.
619
649
  <details>
620
650
  <summary>
621
651
  Example
@@ -668,7 +698,7 @@ There are many advanced types most users don't know about.
668
698
  ```
669
699
  </details>
670
700
 
671
- - [`Omit<T, K>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys) Constructs a type by picking all properties from T and then removing K.
701
+ - [`Omit<T, K>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys) - Constructs a type by picking all properties from T and then removing K.
672
702
  <details>
673
703
  <summary>
674
704
  Example
@@ -20,6 +20,6 @@ async function doSomething(value: Value) {}
20
20
  asyncFunction().then(value => doSomething(value));
21
21
  ```
22
22
 
23
- @category Utilities
23
+ @category Async
24
24
  */
25
25
  export type AsyncReturnType<Target extends AsyncFunction> = PromiseValue<ReturnType<Target>>;
@@ -28,6 +28,6 @@ const getFooAsync: AsyncifiedFooGetter = (someArg) => {
28
28
  }
29
29
  ```
30
30
 
31
- @category Utilities
31
+ @category Async
32
32
  */
33
33
  export type Asyncify<Fn extends (...args: any[]) => any> = SetReturnType<Fn, Promise<PromiseValue<ReturnType<Fn>>>>;
package/source/basic.d.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  /**
2
2
  Matches a [`class`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
3
3
 
4
- @category Basic
4
+ @category Class
5
5
  */
6
6
  export type Class<T, Arguments extends unknown[] = any[]> = Constructor<T, Arguments> & {prototype: T};
7
7
 
8
8
  /**
9
9
  Matches a [`class` constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes).
10
10
 
11
- @category Basic
11
+ @category Class
12
12
  */
13
13
  export type Constructor<T, Arguments extends unknown[] = any[]> = new(...arguments_: Arguments) => T;
14
14
 
@@ -17,21 +17,21 @@ Matches a JSON object.
17
17
 
18
18
  This type can be useful to enforce some input to be JSON-compatible or as a super-type to be extended from. Don't use this as a direct return type as the user would have to double-cast it: `jsonObject as unknown as CustomResponse`. Instead, you could extend your CustomResponse type from it to ensure your type only uses JSON-compatible types: `interface CustomResponse extends JsonObject { … }`.
19
19
 
20
- @category Basic
20
+ @category JSON
21
21
  */
22
22
  export type JsonObject = {[Key in string]?: JsonValue};
23
23
 
24
24
  /**
25
25
  Matches a JSON array.
26
26
 
27
- @category Basic
27
+ @category JSON
28
28
  */
29
29
  export type JsonArray = JsonValue[];
30
30
 
31
31
  /**
32
32
  Matches any valid JSON primitive value.
33
33
 
34
- @category Basic
34
+ @category JSON
35
35
  */
36
36
  export type JsonPrimitive = string | number | boolean | null;
37
37
 
@@ -40,6 +40,6 @@ Matches any valid JSON value.
40
40
 
41
41
  @see `Jsonify` if you need to transform a type to one that is assignable to `JsonValue`.
42
42
 
43
- @category Basic
43
+ @category JSON
44
44
  */
45
45
  export type JsonValue = JsonPrimitive | JsonObject | JsonArray;
@@ -67,6 +67,7 @@ const dbResult: CamelCasedProperties<RawOptions> = {
67
67
  };
68
68
  ```
69
69
 
70
- @category Template Literals
70
+ @category Change case
71
+ @category Template literal
71
72
  */
72
73
  export type CamelCase<K> = K extends string ? CamelCaseStringArray<Split<K extends Uppercase<K> ? Lowercase<K> : K, WordSeparators>> : K;
@@ -38,7 +38,9 @@ const result: CamelCasedPropertiesDeep<UserWithFriends> = {
38
38
  };
39
39
  ```
40
40
 
41
- @category Template Literals
41
+ @category Change case
42
+ @category Template literal
43
+ @category Object
42
44
  */
43
45
  export type CamelCasedPropertiesDeep<Value> = Value extends Function
44
46
  ? Value
@@ -21,7 +21,9 @@ const result: CamelCasedProperties<User> = {
21
21
  };
22
22
  ```
23
23
 
24
- @category Template Literals
24
+ @category Change case
25
+ @category Template literal
26
+ @category Object
25
27
  */
26
28
  export type CamelCasedProperties<Value> = Value extends Function
27
29
  ? Value
@@ -37,7 +37,7 @@ type NonStringKeysOnly = ConditionalExcept<Example, string>;
37
37
  //=> {b: string | number; c: () => void; d: {}}
38
38
  ```
39
39
 
40
- @category Utilities
40
+ @category Object
41
41
  */
42
42
  export type ConditionalExcept<Base, Condition> = Except<
43
43
  Base,
@@ -26,7 +26,7 @@ type StringKeysAndUndefined = ConditionalKeys<Example, string | undefined>;
26
26
  //=> 'a' | 'c'
27
27
  ```
28
28
 
29
- @category Utilities
29
+ @category Object
30
30
  */
31
31
  export type ConditionalKeys<Base, Condition> = NonNullable<
32
32
  // Wrap in `NonNullable` to strip away the `undefined` type from the produced union.
@@ -36,7 +36,7 @@ type StringKeysOnly = ConditionalPick<Example, string>;
36
36
  //=> {a: string}
37
37
  ```
38
38
 
39
- @category Utilities
39
+ @category Object
40
40
  */
41
41
  export type ConditionalPick<Base, Condition> = Pick<
42
42
  Base,
@@ -3,7 +3,7 @@ import {UpperCaseCharacters, WordSeparators} from '../source/utilities';
3
3
  /**
4
4
  Unlike a simpler split, this one includes the delimiter splitted on in the resulting array literal. This is to enable splitting on, for example, upper-case characters.
5
5
 
6
- @category Template Literals
6
+ @category Template literal
7
7
  */
8
8
  export type SplitIncludingDelimiters<Source extends string, Delimiter extends string> =
9
9
  Source extends '' ? [] :
@@ -79,7 +79,8 @@ const rawCliOptions: OddlyCasedProperties<SomeOptions> = {
79
79
  };
80
80
  ```
81
81
 
82
- @category Template Literals
82
+ @category Change case
83
+ @category Template literal
83
84
  */
84
85
  export type DelimiterCase<Value, Delimiter extends string> = Value extends string
85
86
  ? StringArrayToDelimiterCase<
@@ -38,12 +38,14 @@ const result: DelimiterCasedPropertiesDeep<UserWithFriends, '-'> = {
38
38
  };
39
39
  ```
40
40
 
41
- @category Template Literals
41
+ @category Change case
42
+ @category Template literal
43
+ @category Object
42
44
  */
43
45
  export type DelimiterCasedPropertiesDeep<
44
46
  Value,
45
47
  Delimiter extends string,
46
- > = Value extends Function
48
+ > = Value extends Function | Date | RegExp
47
49
  ? Value
48
50
  : Value extends Array<infer U>
49
51
  ? Array<DelimiterCasedPropertiesDeep<U, Delimiter>>
@@ -21,7 +21,9 @@ const result: DelimiterCasedProperties<User, '-'> = {
21
21
  };
22
22
  ```
23
23
 
24
- @category Template Literals
24
+ @category Change case
25
+ @category Template literal
26
+ @category Object
25
27
  */
26
28
  export type DelimiterCasedProperties<
27
29
  Value,
@@ -49,7 +49,10 @@ const setExample = new Set(['a', 1]);
49
49
  const setEntries: Entries<typeof setExample> = [['a', 'a'], [1, 1]];
50
50
  ```
51
51
 
52
- @category Utilities
52
+ @category Object
53
+ @category Map
54
+ @category Set
55
+ @category Array
53
56
  */
54
57
  export type Entries<BaseType> =
55
58
  BaseType extends Map<unknown, unknown> ? MapEntries<BaseType>
package/source/entry.d.ts CHANGED
@@ -52,7 +52,10 @@ const setEntryString: Entry<typeof setExample> = ['a', 'a'];
52
52
  const setEntryNumber: Entry<typeof setExample> = [1, 1];
53
53
  ```
54
54
 
55
- @category Utilities
55
+ @category Object
56
+ @category Map
57
+ @category Array
58
+ @category Set
56
59
  */
57
60
  export type Entry<BaseType> =
58
61
  BaseType extends Map<unknown, unknown> ? MapEntry<BaseType>
@@ -50,7 +50,7 @@ type FooWithoutA = Except<Foo, 'a' | 'c'>;
50
50
  //=> {b: string};
51
51
  ```
52
52
 
53
- @category Utilities
53
+ @category Object
54
54
  */
55
55
  export type Except<ObjectType, KeysType extends keyof ObjectType> = {
56
56
  [KeyType in keyof ObjectType as Filter<KeyType, KeysType>]: ObjectType[KeyType];
@@ -28,7 +28,7 @@ guestFencingTeam.push('Sam');
28
28
  //=> error TS2339: Property 'push' does not exist on type 'FencingTeam'
29
29
  ```
30
30
 
31
- @category Utilities
31
+ @category Array
32
32
  */
33
33
  export type FixedLengthArray<Element, Length extends number, ArrayPrototype = [Element, ...Element[]]> = Pick<
34
34
  ArrayPrototype,
package/source/get.d.ts CHANGED
@@ -1,16 +1,44 @@
1
1
  import {StringDigit} from '../source/utilities';
2
2
  import {Split} from './split';
3
+ import {StringKeyOf} from './string-key-of';
4
+
5
+ type GetOptions = {
6
+ strict?: boolean;
7
+ };
3
8
 
4
9
  /**
5
10
  Like the `Get` type but receives an array of strings as a path parameter.
6
11
  */
7
- type GetWithPath<BaseType, Keys extends readonly string[]> =
12
+ type GetWithPath<BaseType, Keys extends readonly string[], Options extends GetOptions = {}> =
8
13
  Keys extends []
9
14
  ? BaseType
10
15
  : Keys extends [infer Head, ...infer Tail]
11
- ? GetWithPath<PropertyOf<BaseType, Extract<Head, string>>, Extract<Tail, string[]>>
16
+ ? GetWithPath<
17
+ PropertyOf<BaseType, Extract<Head, string>, Options>,
18
+ Extract<Tail, string[]>,
19
+ Options
20
+ >
12
21
  : never;
13
22
 
23
+ /**
24
+ Adds `undefined` to `Type` if `strict` is enabled.
25
+ */
26
+ type Strictify<Type, Options extends GetOptions> =
27
+ Options['strict'] extends true ? Type | undefined : Type;
28
+
29
+ /**
30
+ If `Options['strict']` is `true`, includes `undefined` in the returned type when accessing properties on `Record<string, any>`.
31
+
32
+ Known limitations:
33
+ - Does not include `undefined` in the type on object types with an index signature (for example, `{a: string; [key: string]: string}`).
34
+ */
35
+ type StrictPropertyOf<BaseType, Key extends keyof BaseType, Options extends GetOptions> =
36
+ Record<string, any> extends BaseType
37
+ ? string extends keyof BaseType
38
+ ? Strictify<BaseType[Key], Options> // Record<string, any>
39
+ : BaseType[Key] // Record<'a' | 'b', any> (Records with a string union as keys have required properties)
40
+ : BaseType[Key];
41
+
14
42
  /**
15
43
  Splits a dot-prop style path into a tuple comprised of the properties in the path. Handles square-bracket notation.
16
44
 
@@ -29,8 +57,12 @@ type ToPath<S extends string> = Split<FixPathSquareBrackets<S>, '.'>;
29
57
  Replaces square-bracketed dot notation with dots, for example, `foo[0].bar` -> `foo.0.bar`.
30
58
  */
31
59
  type FixPathSquareBrackets<Path extends string> =
32
- Path extends `${infer Head}[${infer Middle}]${infer Tail}`
33
- ? `${Head}.${Middle}${FixPathSquareBrackets<Tail>}`
60
+ Path extends `[${infer Head}]${infer Tail}`
61
+ ? Tail extends `[${string}`
62
+ ? `${Head}.${FixPathSquareBrackets<Tail>}`
63
+ : `${Head}${FixPathSquareBrackets<Tail>}`
64
+ : Path extends `${infer Head}[${infer Middle}]${infer Tail}`
65
+ ? `${Head}.${FixPathSquareBrackets<`[${Middle}]${Tail}`>}`
34
66
  : Path;
35
67
 
36
68
  /**
@@ -66,7 +98,7 @@ type WithStringsKeys = keyof WithStrings;
66
98
  ```
67
99
  */
68
100
  type WithStringKeys<BaseType extends Record<string | number, any>> = {
69
- [Key in `${Extract<keyof BaseType, string | number>}`]: BaseType[Key]
101
+ [Key in StringKeyOf<BaseType>]: BaseType[Key]
70
102
  };
71
103
 
72
104
  /**
@@ -76,11 +108,11 @@ Note:
76
108
  - Returns `unknown` if `Key` is not a property of `BaseType`, since TypeScript uses structural typing, and it cannot be guaranteed that extra properties unknown to the type system will exist at runtime.
77
109
  - Returns `undefined` from nullish values, to match the behaviour of most deep-key libraries like `lodash`, `dot-prop`, etc.
78
110
  */
79
- type PropertyOf<BaseType, Key extends string> =
111
+ type PropertyOf<BaseType, Key extends string, Options extends GetOptions = {}> =
80
112
  BaseType extends null | undefined
81
113
  ? undefined
82
114
  : Key extends keyof BaseType
83
- ? BaseType[Key]
115
+ ? StrictPropertyOf<BaseType, Key, Options>
84
116
  : BaseType extends [] | [unknown, ...unknown[]]
85
117
  ? unknown // It's a tuple, but `Key` did not extend `keyof BaseType`. So the index is out of bounds.
86
118
  : BaseType extends {
@@ -89,11 +121,11 @@ type PropertyOf<BaseType, Key extends string> =
89
121
  }
90
122
  ? (
91
123
  ConsistsOnlyOf<Key, StringDigit> extends true
92
- ? Item
124
+ ? Strictify<Item, Options>
93
125
  : unknown
94
126
  )
95
127
  : Key extends keyof WithStringKeys<BaseType>
96
- ? WithStringKeys<BaseType>[Key]
128
+ ? StrictPropertyOf<WithStringKeys<BaseType>, Key, Options>
97
129
  : unknown;
98
130
 
99
131
  // This works by first splitting the path based on `.` and `[...]` characters into a tuple of string keys. Then it recursively uses the head key to get the next property of the current object, until there are no keys left. Number keys extract the item type from arrays, or are converted to strings to extract types from tuples and dictionaries with number keys.
@@ -107,7 +139,7 @@ Use-case: Retrieve a property from deep inside an API response or some other com
107
139
  import {Get} from 'type-fest';
108
140
  import * as lodash from 'lodash';
109
141
 
110
- const get = <BaseType, Path extends string>(object: BaseType, path: Path): Get<BaseType, Path> =>
142
+ const get = <BaseType, Path extends string | string[]>(object: BaseType, path: Path): Get<BaseType, Path> =>
111
143
  lodash.get(object, path);
112
144
 
113
145
  interface ApiResponse {
@@ -128,8 +160,20 @@ interface ApiResponse {
128
160
  const getName = (apiResponse: ApiResponse) =>
129
161
  get(apiResponse, 'hits.hits[0]._source.name');
130
162
  //=> Array<{given: string[]; family: string}>
163
+
164
+ // Path also supports an array of strings
165
+ const getNameWithPathArray = (apiResponse: ApiResponse) =>
166
+ get(apiResponse, ['hits','hits', '0', '_source', 'name']);
167
+ //=> Array<{given: string[]; family: string}>
168
+
169
+ // Strict mode:
170
+ Get<string[], '3', {strict: true}> //=> string | undefined
171
+ Get<Record<string, string>, 'foo', {strict: true}> // => string | undefined
131
172
  ```
132
173
 
133
- @category Template Literals
174
+ @category Object
175
+ @category Array
176
+ @category Template literal
134
177
  */
135
- export type Get<BaseType, Path extends string> = GetWithPath<BaseType, ToPath<Path>>;
178
+ export type Get<BaseType, Path extends string | string[], Options extends GetOptions = {}> =
179
+ GetWithPath<BaseType, Path extends string ? ToPath<Path> : Path, Options>;
@@ -12,7 +12,7 @@ import {Includes} from 'type-fest';
12
12
  type hasRed<array extends any[]> = Includes<array, 'red'>;
13
13
  ```
14
14
 
15
- @category Utilities
15
+ @category Array
16
16
  */
17
17
  export type Includes<Value extends readonly any[], Item> =
18
18
  IsEqual<Value[0], Item> extends true
@@ -1,3 +1,5 @@
1
+ import {Primitive} from './primitive';
2
+
1
3
  /**
2
4
  Returns a boolean for whether the two given types are equal.
3
5
 
@@ -35,3 +37,8 @@ the inferred tuple `U` and a tuple of length `B`, then extracts the length of tu
35
37
  export type Subtract<A extends number, B extends number> = BuildTuple<A> extends [...(infer U), ...BuildTuple<B>]
36
38
  ? TupleLength<U>
37
39
  : never;
40
+
41
+ /**
42
+ Matches any primitive, `Date`, or `RegExp` value.
43
+ */
44
+ export type BuiltIns = Primitive | Date | RegExp;
@@ -38,7 +38,7 @@ An example with an array of strings:
38
38
  type MeString = IterableElement<string[]>
39
39
  ```
40
40
 
41
- @category Utilities
41
+ @category Iterable
42
42
  */
43
43
  export type IterableElement<TargetIterable> =
44
44
  TargetIterable extends Iterable<infer ElementType> ?
package/source/join.d.ts CHANGED
@@ -17,7 +17,8 @@ const path: Join<['foo', 'bar', 'baz'], '.'> = ['foo', 'bar', 'baz'].join('.');
17
17
  const path: Join<[1, 2, 3], '.'> = [1, 2, 3].join('.');
18
18
  ```
19
19
 
20
- @category Template Literals
20
+ @category Array
21
+ @category Template literal
21
22
  */
22
23
  export type Join<
23
24
  Strings extends Array<string | number>,
@@ -54,7 +54,7 @@ const timeJson = JSON.parse(JSON.stringify(time)) as Jsonify<typeof time>;
54
54
 
55
55
  @link https://github.com/Microsoft/TypeScript/issues/1897#issuecomment-710744173
56
56
 
57
- @category Utilities
57
+ @category JSON
58
58
  */
59
59
  type Jsonify<T> =
60
60
  // Check if there are any non-JSONable types represented in the union.
@@ -70,6 +70,6 @@ type Jsonify<T> =
70
70
  ? (() => J) extends (() => JsonValue) // Is J assignable to JsonValue?
71
71
  ? J // Then T is Jsonable and its Jsonable value is J
72
72
  : never // Not Jsonable because its toJSON() method does not return JsonValue
73
- : {[P in keyof T]: Jsonify<T[P]>} // It's an object: recursive call for its children
73
+ : {[P in keyof T]: Jsonify<Required<T>[P]>} // It's an object: recursive call for its children
74
74
  : never // Otherwise any other non-object is removed
75
75
  : never; // Otherwise non-JSONable type union was found not empty
@@ -32,6 +32,7 @@ const rawCliOptions: KebabCasedProperties<CliOptions> = {
32
32
  };
33
33
  ```
34
34
 
35
- @category Template Literals
35
+ @category Change case
36
+ @category Template literal
36
37
  */
37
38
  export type KebabCase<Value> = DelimiterCase<Value, '-'>;