type-fest 2.7.0 → 2.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 +7 -0
- package/package.json +1 -1
- package/readme.md +71 -46
- package/source/async-return-type.d.ts +1 -1
- package/source/asyncify.d.ts +1 -1
- package/source/basic.d.ts +6 -6
- package/source/camel-case.d.ts +2 -1
- package/source/camel-cased-properties-deep.d.ts +3 -1
- package/source/camel-cased-properties.d.ts +3 -1
- package/source/conditional-except.d.ts +1 -1
- package/source/conditional-keys.d.ts +1 -1
- package/source/conditional-pick.d.ts +1 -1
- package/source/delimiter-case.d.ts +3 -2
- package/source/delimiter-cased-properties-deep.d.ts +4 -2
- package/source/delimiter-cased-properties.d.ts +3 -1
- package/source/entries.d.ts +4 -1
- package/source/entry.d.ts +4 -1
- package/source/except.d.ts +1 -1
- package/source/fixed-length-array.d.ts +1 -1
- package/source/get.d.ts +57 -13
- package/source/includes.d.ts +1 -1
- package/source/internal.d.ts +7 -0
- package/source/invariant-of.d.ts +72 -0
- package/source/iterable-element.d.ts +1 -1
- package/source/join.d.ts +2 -1
- package/source/jsonify.d.ts +2 -2
- package/source/kebab-case.d.ts +2 -1
- package/source/kebab-cased-properties-deep.d.ts +3 -1
- package/source/kebab-cased-properties.d.ts +3 -1
- package/source/last-array-element.d.ts +2 -1
- package/source/literal-to-primitive.d.ts +36 -0
- package/source/literal-union.d.ts +3 -3
- package/source/merge-exclusive.d.ts +1 -1
- package/source/merge.d.ts +1 -1
- package/source/multidimensional-array.d.ts +1 -1
- package/source/multidimensional-readonly-array.d.ts +1 -1
- package/source/mutable.d.ts +1 -1
- package/source/numeric.d.ts +40 -8
- package/source/observable-like.d.ts +15 -1
- package/source/opaque.d.ts +1 -1
- package/source/package-json.d.ts +1 -1
- package/source/partial-deep.d.ts +7 -4
- package/source/pascal-case.d.ts +2 -1
- package/source/pascal-cased-properties-deep.d.ts +4 -2
- package/source/pascal-cased-properties.d.ts +3 -1
- package/source/primitive.d.ts +1 -1
- package/source/promisable.d.ts +2 -2
- package/source/promise-value.d.ts +1 -1
- package/source/readonly-deep.d.ts +6 -3
- package/source/remove-index-signature.d.ts +98 -0
- package/source/require-all-or-none.d.ts +1 -1
- package/source/require-at-least-one.d.ts +1 -1
- package/source/require-exactly-one.d.ts +1 -1
- package/source/schema.d.ts +72 -0
- package/source/screaming-snake-case.d.ts +2 -1
- package/source/set-optional.d.ts +1 -1
- package/source/set-required.d.ts +1 -1
- package/source/set-return-type.d.ts +1 -1
- package/source/simplify.d.ts +1 -1
- package/source/snake-case.d.ts +2 -1
- package/source/snake-cased-properties-deep.d.ts +3 -1
- package/source/snake-cased-properties.d.ts +3 -1
- package/source/split.d.ts +2 -1
- package/source/string-key-of.d.ts +25 -0
- package/source/stringified.d.ts +1 -1
- package/source/trim.d.ts +2 -1
- package/source/tsconfig-json.d.ts +1 -1
- package/source/typed-array.d.ts +1 -1
- package/source/union-to-intersection.d.ts +1 -1
- package/source/value-of.d.ts +1 -1
package/index.d.ts
CHANGED
|
@@ -12,11 +12,13 @@ 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';
|
|
18
19
|
export {Promisable} from './source/promisable';
|
|
19
20
|
export {Opaque} from './source/opaque';
|
|
21
|
+
export {InvariantOf} from './source/invariant-of';
|
|
20
22
|
export {SetOptional} from './source/set-optional';
|
|
21
23
|
export {SetRequired} from './source/set-required';
|
|
22
24
|
export {ValueOf} from './source/value-of';
|
|
@@ -37,16 +39,21 @@ export {SetReturnType} from './source/set-return-type';
|
|
|
37
39
|
export {Asyncify} from './source/asyncify';
|
|
38
40
|
export {Simplify} from './source/simplify';
|
|
39
41
|
export {Jsonify} from './source/jsonify';
|
|
42
|
+
export {Schema} from './source/schema';
|
|
43
|
+
export {LiteralToPrimitive} from './source/literal-to-primitive';
|
|
40
44
|
export {
|
|
41
45
|
PositiveInfinity,
|
|
42
46
|
NegativeInfinity,
|
|
43
47
|
Finite,
|
|
44
48
|
Integer,
|
|
49
|
+
Float,
|
|
50
|
+
NegativeFloat,
|
|
45
51
|
Negative,
|
|
46
52
|
NonNegative,
|
|
47
53
|
NegativeInteger,
|
|
48
54
|
NonNegativeInteger,
|
|
49
55
|
} from './source/numeric';
|
|
56
|
+
export {StringKeyOf} from './source/string-key-of';
|
|
50
57
|
|
|
51
58
|
// Template literal types
|
|
52
59
|
export {CamelCase} from './source/camel-case';
|
package/package.json
CHANGED
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,63 +99,89 @@ 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/).
|
|
107
|
+
- [`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.
|
|
111
108
|
- [`SetOptional`](source/set-optional.d.ts) - Create a type that makes the given keys optional.
|
|
112
109
|
- [`SetRequired`](source/set-required.d.ts) - Create a type that makes the given keys required.
|
|
113
110
|
- [`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
111
|
- [`ConditionalKeys`](source/conditional-keys.d.ts) - Extract keys from a shape where values extend the given `Condition` type.
|
|
116
112
|
- [`ConditionalPick`](source/conditional-pick.d.ts) - Like `Pick` except it selects properties from a shape where the values extend the given `Condition` type.
|
|
117
113
|
- [`ConditionalExcept`](source/conditional-except.d.ts) - Like `Omit` except it removes properties from a shape where the values extend the given `Condition` type.
|
|
118
114
|
- [`UnionToIntersection`](source/union-to-intersection.d.ts) - Convert a union type to an intersection type.
|
|
115
|
+
- [`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
116
|
- [`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
117
|
- [`IterableElement`](source/iterable-element.d.ts) - Get the element type of an `Iterable`/`AsyncIterable`. For example, an array or a generator.
|
|
124
118
|
- [`Entry`](source/entry.d.ts) - Create a type that represents the type of an entry of a collection.
|
|
125
119
|
- [`Entries`](source/entries.d.ts) - Create a type that represents the type of the entries of a collection.
|
|
126
120
|
- [`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
121
|
- [`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.
|
|
122
|
+
- [`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.
|
|
123
|
+
- [`StringKeyOf`](source/string-key-of.d.ts) - Get keys of the given type as strings.
|
|
124
|
+
- [`Schema`](source/schema.d.ts) - Create a deep version of another object type where property values are recursively replaced into a given value type.
|
|
125
|
+
|
|
126
|
+
### JSON
|
|
127
|
+
|
|
130
128
|
- [`Jsonify`](source/jsonify.d.ts) - Transform a type to one that is assignable to the `JsonValue` type.
|
|
131
|
-
- [`
|
|
132
|
-
- [`
|
|
133
|
-
- [`
|
|
134
|
-
- [`
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
- [`
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
- [`CamelCasedPropertiesDeep`](source/camel-cased-properties-deep.d.ts) – Convert object properties to camel-case recursively (`fooBar`).
|
|
145
|
-
- [`KebabCase`](source/kebab-case.d.ts) – Convert a string literal to kebab-case (`foo-bar`).
|
|
146
|
-
- [`KebabCasedProperties`](source/kebab-cased-properties.d.ts) – Convert a object properties to kebab-case recursively (`foo-bar`).
|
|
147
|
-
- [`KebabCasedPropertiesDeep`](source/kebab-cased-properties-deep.d.ts) – Convert object properties to kebab-case (`foo-bar`).
|
|
148
|
-
- [`PascalCase`](source/pascal-case.d.ts) – Converts a string literal to pascal-case (`FooBar`)
|
|
149
|
-
- [`PascalCasedProperties`](source/pascal-cased-properties.d.ts) – Converts object properties to pascal-case (`FooBar`)
|
|
150
|
-
- [`PascalCasedPropertiesDeep`](source/pascal-cased-properties-deep.d.ts) – Converts object properties to pascal-case (`FooBar`)
|
|
151
|
-
- [`SnakeCase`](source/snake-case.d.ts) – Convert a string literal to snake-case (`foo_bar`).
|
|
152
|
-
- [`SnakeCasedProperties`](source/snake-cased-properties-deep.d.ts) – Convert object properties to snake-case (`foo_bar`).
|
|
153
|
-
- [`SnakeCasedPropertiesDeep`](source/snake-cased-properties-deep.d.ts) – Convert object properties to snake-case recursively (`foo_bar`).
|
|
154
|
-
- [`ScreamingSnakeCase`](source/screaming-snake-case.d.ts) - Convert a string literal to screaming-snake-case (`FOO_BAR`).
|
|
155
|
-
- [`DelimiterCase`](source/delimiter-case.d.ts) – Convert a string literal to a custom string delimiter casing.
|
|
156
|
-
- [`DelimiterCasedProperties`](source/delimiter-cased-properties.d.ts) – Convert object properties to a custom string delimiter casing.
|
|
157
|
-
- [`DelimiterCasedPropertiesDeep`](source/delimiter-cased-properties-deep.d.ts) – Convert object properties to a custom string delimiter casing recursively.
|
|
158
|
-
- [`Join`](source/join.d.ts) - Join an array of strings and/or numbers using the given string as a delimiter.
|
|
159
|
-
- [`Split`](source/split.d.ts) - Represents an array of strings split using a given character or character set.
|
|
129
|
+
- [`JsonPrimitive`](source/basic.d.ts) - Matches a JSON primitive.
|
|
130
|
+
- [`JsonObject`](source/basic.d.ts) - Matches a JSON object.
|
|
131
|
+
- [`JsonArray`](source/basic.d.ts) - Matches a JSON array.
|
|
132
|
+
- [`JsonValue`](source/basic.d.ts) - Matches any valid JSON value.
|
|
133
|
+
|
|
134
|
+
### Async
|
|
135
|
+
|
|
136
|
+
- [`Promisable`](source/promisable.d.ts) - Create a type that represents either the value or the value wrapped in `PromiseLike`.
|
|
137
|
+
- [`AsyncReturnType`](source/async-return-type.d.ts) - Unwrap the return type of a function that returns a `Promise`.
|
|
138
|
+
- [`Asyncify`](source/asyncify.d.ts) - Create an async version of the given function type.
|
|
139
|
+
|
|
140
|
+
### String
|
|
141
|
+
|
|
160
142
|
- [`Trim`](source/trim.d.ts) - Remove leading and trailing spaces from a string.
|
|
161
|
-
- [`
|
|
143
|
+
- [`Split`](source/split.d.ts) - Represents an array of strings split using a given character or character set.
|
|
144
|
+
|
|
145
|
+
### Array
|
|
146
|
+
|
|
147
|
+
- [`Includes`](source/includes.d.ts) - Returns a boolean for whether the given array includes the given item.
|
|
148
|
+
- [`Join`](source/join.d.ts) - Join an array of strings and/or numbers using the given string as a delimiter.
|
|
162
149
|
- [`LastArrayElement`](source/last-array-element.d.ts) - Extracts the type of the last element of an array.
|
|
150
|
+
- [`FixedLengthArray`](source/fixed-length-array.d.ts) - Create a type that represents an array of the given type and length.
|
|
151
|
+
- [`MultidimensionalArray`](source/multidimensional-array.d.ts) - Create a type that represents a multidimensional array of the given type and dimensions.
|
|
152
|
+
- [`MultidimensionalReadonlyArray`](source/multidimensional-readonly-array.d.ts) - Create a type that represents a multidimensional readonly array of the given type and dimensions.
|
|
153
|
+
|
|
154
|
+
### Numeric
|
|
155
|
+
|
|
156
|
+
- [`PositiveInfinity`](source/numeric.d.ts) - Matches the hidden `Infinity` type.
|
|
157
|
+
- [`NegativeInfinity`](source/numeric.d.ts) - Matches the hidden `-Infinity` type.
|
|
158
|
+
- [`Finite`](source/numeric.d.ts) - A finite `number`.
|
|
159
|
+
- [`Integer`](source/numeric.d.ts) - A `number` that is an integer.
|
|
160
|
+
- [`Float`](source/numeric.d.ts) - A `number` that is not an integer.
|
|
161
|
+
- [`NegativeFloat`](source/numeric.d.ts) - A negative (`-∞ < x < 0`) `number` that is not an integer.
|
|
162
|
+
- [`Negative`](source/numeric.d.ts) - A negative `number`/`bigint` (`-∞ < x < 0`)
|
|
163
|
+
- [`NonNegative`](source/numeric.d.ts) - A non-negative `number`/`bigint` (`0 <= x < ∞`).
|
|
164
|
+
- [`NegativeInteger`](source/numeric.d.ts) - A negative (`-∞ < x < 0`) `number` that is an integer.
|
|
165
|
+
- [`NonNegativeInteger`](source/numeric.d.ts) - A non-negative (`0 <= x < ∞`) `number` that is an integer.
|
|
166
|
+
|
|
167
|
+
### Change case
|
|
168
|
+
|
|
169
|
+
- [`CamelCase`](source/camel-case.d.ts) - Convert a string literal to camel-case (`fooBar`).
|
|
170
|
+
- [`CamelCasedProperties`](source/camel-cased-properties.d.ts) - Convert object properties to camel-case (`fooBar`).
|
|
171
|
+
- [`CamelCasedPropertiesDeep`](source/camel-cased-properties-deep.d.ts) - Convert object properties to camel-case recursively (`fooBar`).
|
|
172
|
+
- [`KebabCase`](source/kebab-case.d.ts) - Convert a string literal to kebab-case (`foo-bar`).
|
|
173
|
+
- [`KebabCasedProperties`](source/kebab-cased-properties.d.ts) - Convert a object properties to kebab-case recursively (`foo-bar`).
|
|
174
|
+
- [`KebabCasedPropertiesDeep`](source/kebab-cased-properties-deep.d.ts) - Convert object properties to kebab-case (`foo-bar`).
|
|
175
|
+
- [`PascalCase`](source/pascal-case.d.ts) - Converts a string literal to pascal-case (`FooBar`)
|
|
176
|
+
- [`PascalCasedProperties`](source/pascal-cased-properties.d.ts) - Converts object properties to pascal-case (`FooBar`)
|
|
177
|
+
- [`PascalCasedPropertiesDeep`](source/pascal-cased-properties-deep.d.ts) - Converts object properties to pascal-case (`FooBar`)
|
|
178
|
+
- [`SnakeCase`](source/snake-case.d.ts) - Convert a string literal to snake-case (`foo_bar`).
|
|
179
|
+
- [`SnakeCasedProperties`](source/snake-cased-properties-deep.d.ts) - Convert object properties to snake-case (`foo_bar`).
|
|
180
|
+
- [`SnakeCasedPropertiesDeep`](source/snake-cased-properties-deep.d.ts) - Convert object properties to snake-case recursively (`foo_bar`).
|
|
181
|
+
- [`ScreamingSnakeCase`](source/screaming-snake-case.d.ts) - Convert a string literal to screaming-snake-case (`FOO_BAR`).
|
|
182
|
+
- [`DelimiterCase`](source/delimiter-case.d.ts) - Convert a string literal to a custom string delimiter casing.
|
|
183
|
+
- [`DelimiterCasedProperties`](source/delimiter-cased-properties.d.ts) - Convert object properties to a custom string delimiter casing.
|
|
184
|
+
- [`DelimiterCasedPropertiesDeep`](source/delimiter-cased-properties-deep.d.ts) - Convert object properties to a custom string delimiter casing recursively.
|
|
163
185
|
|
|
164
186
|
### Miscellaneous
|
|
165
187
|
|
|
@@ -170,17 +192,20 @@ Click the type names for complete docs.
|
|
|
170
192
|
|
|
171
193
|
*If we decline a type addition, we will make sure to document the better solution here.*
|
|
172
194
|
|
|
173
|
-
- [`Diff` and `Spread`](https://github.com/sindresorhus/type-fest/pull/7) - The
|
|
195
|
+
- [`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.
|
|
174
196
|
- [`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.
|
|
175
197
|
- [`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.
|
|
176
198
|
- [`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.
|
|
177
|
-
- [`Nullish`](https://github.com/sindresorhus/type-fest/pull/318) - The type only saves a couple of characters, not everyone knows what
|
|
199
|
+
- [`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).
|
|
200
|
+
- [`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.
|
|
201
|
+
- [`ExtendOr` and `ExtendAnd`](https://github.com/sindresorhus/type-fest/pull/247) - The benefits don't outweigh having to learn what they mean.
|
|
178
202
|
|
|
179
203
|
## Alternative type names
|
|
180
204
|
|
|
181
205
|
*If you know one of our types by a different name, add it here for discovery.*
|
|
182
206
|
|
|
183
207
|
- `PartialBy` - See [`SetOptional`](https://github.com/sindresorhus/type-fest/blob/main/source/set-optional.d.ts)
|
|
208
|
+
- `RecordDeep`- See [`Schema`](https://github.com/sindresorhus/type-fest/blob/main/source/schema.d.ts)
|
|
184
209
|
|
|
185
210
|
## Tips
|
|
186
211
|
|
|
@@ -589,7 +614,7 @@ There are many advanced types most users don't know about.
|
|
|
589
614
|
```
|
|
590
615
|
</details>
|
|
591
616
|
|
|
592
|
-
- [`ReturnType<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#returntypetype)
|
|
617
|
+
- [`ReturnType<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#returntypetype) - Obtain the return type of a function type.
|
|
593
618
|
<details>
|
|
594
619
|
<summary>
|
|
595
620
|
Example
|
|
@@ -624,7 +649,7 @@ There are many advanced types most users don't know about.
|
|
|
624
649
|
```
|
|
625
650
|
</details>
|
|
626
651
|
|
|
627
|
-
- [`InstanceType<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#instancetypetype)
|
|
652
|
+
- [`InstanceType<T>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#instancetypetype) - Obtain the instance type of a constructor function type.
|
|
628
653
|
<details>
|
|
629
654
|
<summary>
|
|
630
655
|
Example
|
|
@@ -677,7 +702,7 @@ There are many advanced types most users don't know about.
|
|
|
677
702
|
```
|
|
678
703
|
</details>
|
|
679
704
|
|
|
680
|
-
- [`Omit<T, K>`](https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys)
|
|
705
|
+
- [`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.
|
|
681
706
|
<details>
|
|
682
707
|
<summary>
|
|
683
708
|
Example
|
package/source/asyncify.d.ts
CHANGED
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
43
|
+
@category JSON
|
|
44
44
|
*/
|
|
45
45
|
export type JsonValue = JsonPrimitive | JsonObject | JsonArray;
|
package/source/camel-case.d.ts
CHANGED
|
@@ -67,6 +67,7 @@ const dbResult: CamelCasedProperties<RawOptions> = {
|
|
|
67
67
|
};
|
|
68
68
|
```
|
|
69
69
|
|
|
70
|
-
@category
|
|
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
|
|
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
|
|
@@ -26,7 +26,7 @@ type StringKeysAndUndefined = ConditionalKeys<Example, string | undefined>;
|
|
|
26
26
|
//=> 'a' | 'c'
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
@category
|
|
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.
|
|
@@ -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
|
|
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
|
|
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
|
|
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>>
|
package/source/entries.d.ts
CHANGED
|
@@ -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
|
|
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
|
|
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>
|
package/source/except.d.ts
CHANGED
|
@@ -50,7 +50,7 @@ type FooWithoutA = Except<Foo, 'a' | 'c'>;
|
|
|
50
50
|
//=> {b: string};
|
|
51
51
|
```
|
|
52
52
|
|
|
53
|
-
@category
|
|
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
|
|
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
|
-
: Keys extends [infer Head, ...infer Tail]
|
|
11
|
-
? GetWithPath<
|
|
15
|
+
: Keys extends readonly [infer Head, ...infer Tail]
|
|
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
|
|
33
|
-
?
|
|
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
|
|
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
|
|
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>
|
|
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 | readonly 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 a readonly array of strings
|
|
165
|
+
const getNameWithPathArray = (apiResponse: ApiResponse) =>
|
|
166
|
+
get(apiResponse, ['hits','hits', '0', '_source', 'name'] as const);
|
|
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
|
|
174
|
+
@category Object
|
|
175
|
+
@category Array
|
|
176
|
+
@category Template literal
|
|
134
177
|
*/
|
|
135
|
-
export type Get<BaseType, Path extends string
|
|
178
|
+
export type Get<BaseType, Path extends string | readonly string[], Options extends GetOptions = {}> =
|
|
179
|
+
GetWithPath<BaseType, Path extends string ? ToPath<Path> : Path, Options>;
|
package/source/includes.d.ts
CHANGED
package/source/internal.d.ts
CHANGED
|
@@ -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;
|