@alessiofrittoli/type-utils 1.0.1 → 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 +7 -7
- package/scripts/postinstall/ts-setup.js +4 -3
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.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Lightweight TypeScript types utilities",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Alessio Frittoli",
|
|
@@ -26,17 +26,17 @@
|
|
|
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
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@eslint/js": "^9.
|
|
35
|
-
"@types/node": "^22.10.
|
|
36
|
-
"eslint": "^9.
|
|
37
|
-
"globals": "^15.
|
|
34
|
+
"@eslint/js": "^9.17.0",
|
|
35
|
+
"@types/node": "^22.10.2",
|
|
36
|
+
"eslint": "^9.17.0",
|
|
37
|
+
"globals": "^15.14.0",
|
|
38
38
|
"tsup": "^8.3.5",
|
|
39
39
|
"typescript": "^5.7.2",
|
|
40
|
-
"typescript-eslint": "^8.
|
|
40
|
+
"typescript-eslint": "^8.18.1"
|
|
41
41
|
}
|
|
42
42
|
}
|
|
@@ -7,6 +7,7 @@ const tsSetup = () => {
|
|
|
7
7
|
|
|
8
8
|
const projectName = project.name.replace( '@alessiofrittoli/', '' )
|
|
9
9
|
const projectRoot = process.env.INIT_CWD || process.cwd()
|
|
10
|
+
const typesFilename = `${ projectName }.d.ts`
|
|
10
11
|
|
|
11
12
|
if ( projectRoot.endsWith( projectName ) ) {
|
|
12
13
|
console.log( `Skip "postinstall" script. Running in ${ project.name }` )
|
|
@@ -17,7 +18,7 @@ const tsSetup = () => {
|
|
|
17
18
|
process.exit( 1 )
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
const tsconfigPath = path.resolve( projectRoot,
|
|
21
|
+
const tsconfigPath = path.resolve( projectRoot, typesFilename )
|
|
21
22
|
|
|
22
23
|
const data = [
|
|
23
24
|
`/// <reference types="${ project.name }" />`,
|
|
@@ -26,10 +27,10 @@ const tsSetup = () => {
|
|
|
26
27
|
|
|
27
28
|
try {
|
|
28
29
|
fs.writeFileSync( tsconfigPath, data )
|
|
29
|
-
console.log(
|
|
30
|
+
console.log( `"${ typesFilename }" has been created at the root of your project.` )
|
|
30
31
|
console.log( 'Please update your tsconfig.json to add this file in your "include" property of your tsconfig.json file.' )
|
|
31
32
|
} catch ( error ) {
|
|
32
|
-
console.error(
|
|
33
|
+
console.error( `An error occured while creating "${ typesFilename }" at the root of your project. Some global types may not working as expect.`, error )
|
|
33
34
|
process.exit( 1 )
|
|
34
35
|
}
|
|
35
36
|
|