type-fest 2.5.1 → 2.5.2
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/package.json +1 -1
- package/readme.md +1 -1
- package/source/delimiter-case.d.ts +5 -3
- package/source/simplify.d.ts +35 -1
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -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
|
|
@@ -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,9 @@ 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>}`
|
|
42
43
|
: '';
|
|
43
44
|
|
|
44
45
|
/**
|
|
@@ -81,6 +82,7 @@ const rawCliOptions: OddlyCasedProperties<SomeOptions> = {
|
|
|
81
82
|
export type DelimiterCase<Value, Delimiter extends string> = Value extends string
|
|
82
83
|
? StringArrayToDelimiterCase<
|
|
83
84
|
SplitIncludingDelimiters<Value, WordSeparators | UpperCaseCharacters>,
|
|
85
|
+
true,
|
|
84
86
|
WordSeparators,
|
|
85
87
|
UpperCaseCharacters,
|
|
86
88
|
Delimiter
|
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]};
|