@alessiofrittoli/type-utils 1.1.0 → 1.2.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/README.md +1 -1
- package/dist/index.d.mts +45 -0
- package/dist/index.d.ts +45 -0
- package/package.json +2 -2
package/README.md
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -60,6 +60,51 @@ declare global {
|
|
|
60
60
|
type NonNullableFields<T> = {
|
|
61
61
|
[P in keyof T]: NonNullable<T[P]>;
|
|
62
62
|
};
|
|
63
|
+
/**
|
|
64
|
+
* Get the type of the value of merged enums.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
*
|
|
68
|
+
* ```ts
|
|
69
|
+
* enum Mammals {
|
|
70
|
+
* Humans,
|
|
71
|
+
* Bats,
|
|
72
|
+
* Dolphins
|
|
73
|
+
* }
|
|
74
|
+
*
|
|
75
|
+
* enum Reptiles {
|
|
76
|
+
* Snakes,
|
|
77
|
+
* Alligators,
|
|
78
|
+
* Lizards
|
|
79
|
+
* }
|
|
80
|
+
*
|
|
81
|
+
* // merged enums.
|
|
82
|
+
* const Animals = { Mammals, Reptiles }
|
|
83
|
+
* type Animals = typeof Animals
|
|
84
|
+
*
|
|
85
|
+
* type Animal = MergedEnumValueType<typeof Animals>
|
|
86
|
+
*
|
|
87
|
+
* const testAnimal: Animal = 0 as any
|
|
88
|
+
*
|
|
89
|
+
* switch ( testAnimal ) {
|
|
90
|
+
* case Animals.Mammals.Humans:
|
|
91
|
+
* case Animals.Mammals.Bats:
|
|
92
|
+
* case Animals.Mammals.Dolphins:
|
|
93
|
+
* case Animals.Reptiles.Snakes:
|
|
94
|
+
* case Animals.Reptiles.Alligators:
|
|
95
|
+
* case Animals.Reptiles.Lizards:
|
|
96
|
+
* break
|
|
97
|
+
* default: {
|
|
98
|
+
* const invalid = testAnimal // `testAnimal` is type of never
|
|
99
|
+
* }
|
|
100
|
+
* }
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
type MergedEnumValue<T> = {
|
|
104
|
+
[K in keyof T]: {
|
|
105
|
+
[K2 in keyof T[K]]: T[K][K2];
|
|
106
|
+
}[keyof T[K]];
|
|
107
|
+
}[keyof T];
|
|
63
108
|
}
|
|
64
109
|
|
|
65
110
|
type DoNotUse = never;
|
package/dist/index.d.ts
CHANGED
|
@@ -60,6 +60,51 @@ declare global {
|
|
|
60
60
|
type NonNullableFields<T> = {
|
|
61
61
|
[P in keyof T]: NonNullable<T[P]>;
|
|
62
62
|
};
|
|
63
|
+
/**
|
|
64
|
+
* Get the type of the value of merged enums.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
*
|
|
68
|
+
* ```ts
|
|
69
|
+
* enum Mammals {
|
|
70
|
+
* Humans,
|
|
71
|
+
* Bats,
|
|
72
|
+
* Dolphins
|
|
73
|
+
* }
|
|
74
|
+
*
|
|
75
|
+
* enum Reptiles {
|
|
76
|
+
* Snakes,
|
|
77
|
+
* Alligators,
|
|
78
|
+
* Lizards
|
|
79
|
+
* }
|
|
80
|
+
*
|
|
81
|
+
* // merged enums.
|
|
82
|
+
* const Animals = { Mammals, Reptiles }
|
|
83
|
+
* type Animals = typeof Animals
|
|
84
|
+
*
|
|
85
|
+
* type Animal = MergedEnumValueType<typeof Animals>
|
|
86
|
+
*
|
|
87
|
+
* const testAnimal: Animal = 0 as any
|
|
88
|
+
*
|
|
89
|
+
* switch ( testAnimal ) {
|
|
90
|
+
* case Animals.Mammals.Humans:
|
|
91
|
+
* case Animals.Mammals.Bats:
|
|
92
|
+
* case Animals.Mammals.Dolphins:
|
|
93
|
+
* case Animals.Reptiles.Snakes:
|
|
94
|
+
* case Animals.Reptiles.Alligators:
|
|
95
|
+
* case Animals.Reptiles.Lizards:
|
|
96
|
+
* break
|
|
97
|
+
* default: {
|
|
98
|
+
* const invalid = testAnimal // `testAnimal` is type of never
|
|
99
|
+
* }
|
|
100
|
+
* }
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
type MergedEnumValue<T> = {
|
|
104
|
+
[K in keyof T]: {
|
|
105
|
+
[K2 in keyof T[K]]: T[K][K2];
|
|
106
|
+
}[keyof T[K]];
|
|
107
|
+
}[keyof T];
|
|
63
108
|
}
|
|
64
109
|
|
|
65
110
|
type DoNotUse = never;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alessiofrittoli/type-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Lightweight TypeScript types utilities",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Alessio Frittoli",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"dist", "scripts"
|
|
27
27
|
],
|
|
28
28
|
"scripts": {
|
|
29
|
-
"build": "
|
|
29
|
+
"build": "pnpm lint && tsup",
|
|
30
30
|
"lint": "eslint",
|
|
31
31
|
"postinstall": "node scripts/postinstall/index.js"
|
|
32
32
|
},
|