type-fest 2.5.0 → 2.5.4
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 +1 -9
- package/package.json +2 -2
- package/readme.md +10 -4
- package/source/delimiter-case.d.ts +7 -3
- package/source/join.d.ts +1 -1
- package/source/partial-deep.d.ts +5 -1
- package/source/simplify.d.ts +35 -1
package/license
CHANGED
|
@@ -1,9 +1 @@
|
|
|
1
|
-
MIT
|
|
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.
|
|
3
|
+
"version": "2.5.4",
|
|
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": "
|
|
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
|
-
|
|
61
|
+
```sh
|
|
62
|
+
npm install type-fest
|
|
63
63
|
```
|
|
64
64
|
|
|
65
65
|
*Requires TypeScript >=4.2*
|
|
@@ -127,7 +127,7 @@ Click the type names for complete docs.
|
|
|
127
127
|
- [`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
128
|
- [`Asyncify`](source/asyncify.d.ts) - Create an async version of the given function type.
|
|
129
129
|
- [`Includes`](source/includes.d.ts) - Returns a boolean for whether the given array includes the given item.
|
|
130
|
-
- [`Simplify`](source/simplify.d.ts) -
|
|
130
|
+
- [`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
131
|
- [`Jsonify`](source/jsonify.d.ts) - Transform a type to one that is assignable to the `JsonValue` type.
|
|
132
132
|
|
|
133
133
|
### Template literal types
|
|
@@ -168,6 +168,12 @@ Click the type names for complete docs.
|
|
|
168
168
|
- [`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
169
|
- [`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
170
|
|
|
171
|
+
## Alternative type names
|
|
172
|
+
|
|
173
|
+
*If you know one of our types by a different name, add it here for discovery.*
|
|
174
|
+
|
|
175
|
+
- `PartialBy` - See [`SetOptional`](https://github.com/sindresorhus/type-fest/blob/main/source/set-optional.d.ts)
|
|
176
|
+
|
|
171
177
|
## Tips
|
|
172
178
|
|
|
173
179
|
### Related
|
|
@@ -787,7 +793,7 @@ You can find some examples in the [TypeScript docs](https://www.typescriptlang.o
|
|
|
787
793
|
|
|
788
794
|
## License
|
|
789
795
|
|
|
790
|
-
(MIT OR CC0-1.0)
|
|
796
|
+
SPDX-License-Identifier: (MIT OR CC0-1.0)
|
|
791
797
|
|
|
792
798
|
---
|
|
793
799
|
|
|
@@ -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
|
|
29
|
+
string;
|
package/source/partial-deep.d.ts
CHANGED
|
@@ -43,7 +43,11 @@ export type PartialDeep<T> = T extends Primitive
|
|
|
43
43
|
: T extends ((...arguments: any[]) => unknown)
|
|
44
44
|
? T | undefined
|
|
45
45
|
: T extends object
|
|
46
|
-
?
|
|
46
|
+
? T extends Array<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
|
|
47
|
+
? ItemType[] extends T // Test for arrays (non-tuples) specifically
|
|
48
|
+
? Array<PartialDeep<ItemType | undefined>> // Recreate relevant array type to prevent eager evaluation of circular reference
|
|
49
|
+
: PartialObjectDeep<T> // Tuples behave properly
|
|
50
|
+
: PartialObjectDeep<T>
|
|
47
51
|
: unknown;
|
|
48
52
|
|
|
49
53
|
/**
|
package/source/simplify.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
|
|
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]};
|