type-fest 2.5.1 → 2.6.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/license CHANGED
@@ -1,9 +1 @@
1
- MIT License
2
-
3
- Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
-
7
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
-
9
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ SPDX-License-Identifier: (MIT OR CC0-1.0)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-fest",
3
- "version": "2.5.1",
3
+ "version": "2.6.0",
4
4
  "description": "A collection of essential TypeScript types",
5
5
  "license": "(MIT OR CC0-1.0)",
6
6
  "repository": "sindresorhus/type-fest",
@@ -35,7 +35,7 @@
35
35
  "@sindresorhus/tsconfig": "~0.7.0",
36
36
  "expect-type": "^0.12.0",
37
37
  "tsd": "^0.17.0",
38
- "typescript": "^4.1.3",
38
+ "typescript": ">=4.2",
39
39
  "xo": "^0.43.0"
40
40
  },
41
41
  "types": "./index.d.ts",
package/readme.md CHANGED
@@ -58,8 +58,8 @@ PR welcome for additional commonly needed types and docs improvements. Read the
58
58
 
59
59
  ## Install
60
60
 
61
- ```
62
- $ npm install type-fest
61
+ ```sh
62
+ npm install type-fest
63
63
  ```
64
64
 
65
65
  *Requires TypeScript >=4.2*
@@ -111,7 +111,6 @@ Click the type names for complete docs.
111
111
  - [`SetOptional`](source/set-optional.d.ts) - Create a type that makes the given keys optional.
112
112
  - [`SetRequired`](source/set-required.d.ts) - Create a type that makes the given keys required.
113
113
  - [`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
- - [`PromiseValue`](source/promise-value.d.ts) - Returns the type that is wrapped inside a `Promise`.
115
114
  - [`AsyncReturnType`](source/async-return-type.d.ts) - Unwrap the return type of a function that returns a `Promise`.
116
115
  - [`ConditionalKeys`](source/conditional-keys.d.ts) - Extract keys from a shape where values extend the given `Condition` type.
117
116
  - [`ConditionalPick`](source/conditional-pick.d.ts) - Like `Pick` except it selects properties from a shape where the values extend the given `Condition` type.
@@ -127,7 +126,7 @@ Click the type names for complete docs.
127
126
  - [`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.
128
127
  - [`Asyncify`](source/asyncify.d.ts) - Create an async version of the given function type.
129
128
  - [`Includes`](source/includes.d.ts) - Returns a boolean for whether the given array includes the given item.
130
- - [`Simplify`](source/simplify.d.ts) - Flatten the type output to improve type hints shown in editors.
129
+ - [`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.
131
130
  - [`Jsonify`](source/jsonify.d.ts) - Transform a type to one that is assignable to the `JsonValue` type.
132
131
 
133
132
  ### Template literal types
@@ -168,6 +167,12 @@ Click the type names for complete docs.
168
167
  - [`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.
169
168
  - [`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.
170
169
 
170
+ ## Alternative type names
171
+
172
+ *If you know one of our types by a different name, add it here for discovery.*
173
+
174
+ - `PartialBy` - See [`SetOptional`](https://github.com/sindresorhus/type-fest/blob/main/source/set-optional.d.ts)
175
+
171
176
  ## Tips
172
177
 
173
178
  ### Related
@@ -787,7 +792,7 @@ You can find some examples in the [TypeScript docs](https://www.typescriptlang.o
787
792
 
788
793
  ## License
789
794
 
790
- (MIT OR CC0-1.0)
795
+ SPDX-License-Identifier: (MIT OR CC0-1.0)
791
796
 
792
797
  ---
793
798
 
@@ -24,8 +24,9 @@ Format a specific part of the splitted string literal that `StringArrayToDelimit
24
24
 
25
25
  @see StringArrayToDelimiterCase
26
26
  */
27
- type StringPartToDelimiterCase<StringPart extends string, UsedWordSeparators extends string, UsedUpperCaseCharacters extends string, Delimiter extends string> =
27
+ type StringPartToDelimiterCase<StringPart extends string, Start extends boolean, UsedWordSeparators extends string, UsedUpperCaseCharacters extends string, Delimiter extends string> =
28
28
  StringPart extends UsedWordSeparators ? Delimiter :
29
+ Start extends true ? Lowercase<StringPart> :
29
30
  StringPart extends UsedUpperCaseCharacters ? `${Delimiter}${Lowercase<StringPart>}` :
30
31
  StringPart;
31
32
 
@@ -36,9 +37,11 @@ It receives `UsedWordSeparators` and `UsedUpperCaseCharacters` as input to ensur
36
37
 
37
38
  @see SplitIncludingDelimiters
38
39
  */
39
- type StringArrayToDelimiterCase<Parts extends readonly any[], UsedWordSeparators extends string, UsedUpperCaseCharacters extends string, Delimiter extends string> =
40
+ type StringArrayToDelimiterCase<Parts extends readonly any[], Start extends boolean, UsedWordSeparators extends string, UsedUpperCaseCharacters extends string, Delimiter extends string> =
40
41
  Parts extends [`${infer FirstPart}`, ...infer RemainingParts]
41
- ? `${StringPartToDelimiterCase<FirstPart, UsedWordSeparators, UsedUpperCaseCharacters, Delimiter>}${StringArrayToDelimiterCase<RemainingParts, UsedWordSeparators, UsedUpperCaseCharacters, Delimiter>}`
42
+ ? `${StringPartToDelimiterCase<FirstPart, Start, UsedWordSeparators, UsedUpperCaseCharacters, Delimiter>}${StringArrayToDelimiterCase<RemainingParts, false, UsedWordSeparators, UsedUpperCaseCharacters, Delimiter>}`
43
+ : Parts extends [string]
44
+ ? string
42
45
  : '';
43
46
 
44
47
  /**
@@ -81,6 +84,7 @@ const rawCliOptions: OddlyCasedProperties<SomeOptions> = {
81
84
  export type DelimiterCase<Value, Delimiter extends string> = Value extends string
82
85
  ? StringArrayToDelimiterCase<
83
86
  SplitIncludingDelimiters<Value, WordSeparators | UpperCaseCharacters>,
87
+ true,
84
88
  WordSeparators,
85
89
  UpperCaseCharacters,
86
90
  Delimiter
package/source/join.d.ts CHANGED
@@ -26,4 +26,4 @@ export type Join<
26
26
  Strings extends [string | number] ? `${Strings[0]}` :
27
27
  // @ts-expect-error `Rest` is inferred as `unknown` here: https://github.com/microsoft/TypeScript/issues/45281
28
28
  Strings extends [string | number, ...infer Rest] ? `${Strings[0]}${Delimiter}${Join<Rest, Delimiter>}` :
29
- string | number;
29
+ string;
@@ -1,4 +1,6 @@
1
1
  /**
2
+ @deprecated Use the built-in [`Awaited` type](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-5.html#the-awaited-type-and-promise-improvements) instead.
3
+
2
4
  Returns the type that is wrapped inside a `Promise` type.
3
5
  If the type is a nested Promise, it is unwrapped recursively until a non-Promise type is obtained.
4
6
  If the type is not a `Promise`, the type itself is returned.
@@ -1,5 +1,5 @@
1
1
  /**
2
- Flatten the type output to improve type hints shown in editors.
2
+ 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.
3
3
 
4
4
  @example
5
5
  ```
@@ -19,6 +19,40 @@ type SizeProps = {
19
19
  type Props = Simplify<PositionProps & SizeProps>;
20
20
  ```
21
21
 
22
+ Sometimes it is desired to pass a value as a function argument that has a different type. At first inspection it may seem assignable, and then you discover it is not because the `value`'s type definition was defined as an interface. In the following example, `fn` requires an argument of type `Record<string, unknown>`. If the value is defined as a literal, then it is assignable. And if the `value` is defined as type using the `Simplify` utility the value is assignable. But if the `value` is defined as an interface, it is not assignable because the interface is not sealed and elsewhere a non-string property could be added to the interface.
23
+
24
+ If the type definition must be an interface (perhaps it was defined in a third-party npm package), then the `value` can be defined as `const value: Simplify<SomeInterface> = ...`. Then `value` will be assignable to the `fn` argument. Or the `value` can be cast as `Simplify<SomeInterface>` if you can't re-declare the `value`.
25
+
26
+ @example
27
+ ```
28
+ import {Simplify} from 'type-fest';
29
+
30
+ interface SomeInterface {
31
+ foo: number;
32
+ bar?: string;
33
+ baz: number | undefined;
34
+ }
35
+
36
+ type SomeType = {
37
+ foo: number;
38
+ bar?: string;
39
+ baz: number | undefined;
40
+ };
41
+
42
+ const literal = {foo: 123, bar: 'hello', baz: 456};
43
+ const someType: SomeType = literal;
44
+ const someInterface: SomeInterface = literal;
45
+
46
+ function fn(object: Record<string, unknown>): void {}
47
+
48
+ fn(literal); // Good: literal object type is sealed
49
+ fn(someType); // Good: type is sealed
50
+ fn(someInterface); // Error: Index signature for type 'string' is missing in type 'someInterface'. Because `interface` can be re-opened
51
+ fn(someInterface as Simplify<SomeInterface>); // Good: transform an `interface` into a `type`
52
+ ```
53
+
54
+ @link https://github.com/microsoft/TypeScript/issues/15300
55
+
22
56
  @category Utilities
23
57
  */
24
58
  export type Simplify<T> = {[KeyType in keyof T]: T[KeyType]};
@@ -15,6 +15,7 @@ declare namespace TsConfigJson {
15
15
  | 'ES6'
16
16
  | 'ES2015'
17
17
  | 'ES2020'
18
+ | 'ES2022'
18
19
  | 'ESNext'
19
20
  | 'None'
20
21
  // Lowercase alternatives
@@ -25,6 +26,7 @@ declare namespace TsConfigJson {
25
26
  | 'es6'
26
27
  | 'es2015'
27
28
  | 'es2020'
29
+ | 'es2022'
28
30
  | 'esnext'
29
31
  | 'none';
30
32