Skip to content

Commit 0a94d3e

Browse files
add prettier and eslint
1 parent f870498 commit 0a94d3e

File tree

7 files changed

+1149
-31
lines changed

7 files changed

+1149
-31
lines changed

.husky/pre-commit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
npx lint-staged

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all"
4+
}

eslint.config.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import globals from 'globals';
2+
import pluginJs from '@eslint/js';
3+
import tseslint from 'typescript-eslint';
4+
5+
export default [
6+
{ files: ['**/*.{js,mjs,cjs,ts}'] },
7+
{ languageOptions: { globals: globals.browser } },
8+
pluginJs.configs.recommended,
9+
...tseslint.configs.recommended,
10+
];

package.json

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"name": "ts-nester",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "A TypeScript utility for converting nested JSON objects into TypeScript interfaces, optimized for i18n applications.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"scripts": {
88
"test": "echo \"Error: no test specified\" && exit 1",
9-
"build": "tsc"
9+
"build": "tsc",
10+
"prepare": "husky"
1011
},
1112
"repository": {
1213
"type": "git",
@@ -27,9 +28,20 @@
2728
},
2829
"homepage": "https://github.com/phamquyetthang/ts-nester#readme",
2930
"devDependencies": {
30-
"typescript": "^5.4.5"
31+
"@eslint/js": "^9.6.0",
32+
"eslint": "9.x",
33+
"globals": "^15.8.0",
34+
"husky": "^9.0.11",
35+
"lint-staged": "^15.2.7",
36+
"prettier": "3.3.2",
37+
"typescript": "^5.4.5",
38+
"typescript-eslint": "^7.16.0"
3139
},
3240
"dependencies": {
3341
"@types/node": "^20.14.2"
42+
},
43+
"lint-staged": {
44+
"**/*": "prettier --write --ignore-unknown",
45+
"**/*.{ts}": "eslint --cache --cache-location ./node_modules/.cache/eslint --fix ."
3446
}
3547
}

utils/FlattenedTypePaths.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DotNestedKeys } from "./dotNestedKey";
1+
import { DotNestedKeys } from './dotNestedKey';
22

33
/**
44
* Generates a flattened representation of an object's type structure, mapping each nested path to its corresponding value type.
@@ -24,7 +24,6 @@ export type FlattenedTypePaths<T> = {
2424
: never;
2525
};
2626

27-
2827
/**
2928
* Recursively generates a flattened representation of an object's type structure, mapping each nested path to its corresponding value type up to a specified depth.
3029
* This utility type iterates over each key in the object, and for objects at each level, it recursively applies itself to flatten the structure.
@@ -46,17 +45,18 @@ export type FlattenedTypePaths<T> = {
4645
* }
4746
* }
4847
* };
49-
*
48+
*
5049
* // Flatten up to depth 2
5150
* type ResultDepth2 = DeepFlattenedTypePaths<typeof example, 2>;
5251
* // Expected type: { 'b.c': number, 'b.d': number, 'b.e': { f: number, g: { k: number, l: string } } }
53-
*
52+
*
5453
* // Flatten up to depth 3
5554
* type ResultDepth3 = DeepFlattenedTypePaths<typeof example, 3>;
5655
* // Expected type: { 'b.c': number, 'b.d': number, 'b.e.f': number, 'b.e.g.k': number, 'b.e.g.l': string }
5756
*/
5857
export type DeepFlattenedTypePaths<T, Depth extends number> = Depth extends 0
59-
? {}
58+
? // eslint-disable-next-line @typescript-eslint/ban-types
59+
{}
6060
: {
6161
[K in keyof T]: T[K] extends object
6262
? {
@@ -74,26 +74,28 @@ export type DeepFlattenedTypePaths<T, Depth extends number> = Depth extends 0
7474
? {
7575
[K in keyof T]: T[K];
7676
}
77-
: {});
77+
: // eslint-disable-next-line @typescript-eslint/ban-types
78+
{});
7879

80+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
7981
type Subtract<N extends number, M extends number> = N extends 1
8082
? 0
8183
: N extends 2
82-
? 1
83-
: N extends 3
84-
? 2
85-
: N extends 4
86-
? 3
87-
: N extends 5
88-
? 4
89-
: N extends 6
90-
? 5
91-
: N extends 7
92-
? 6
93-
: N extends 8
94-
? 7
95-
: N extends 9
96-
? 8
97-
: N extends 10
98-
? 9
99-
: never;
84+
? 1
85+
: N extends 3
86+
? 2
87+
: N extends 4
88+
? 3
89+
: N extends 5
90+
? 4
91+
: N extends 6
92+
? 5
93+
: N extends 7
94+
? 6
95+
: N extends 8
96+
? 7
97+
: N extends 9
98+
? 8
99+
: N extends 10
100+
? 9
101+
: never;

utils/childType.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
* type BarItem = ChildItemType<Obj, "bar"> // { baz: number }
1818
* type BazItem = ChildItemType<Obj, "baz"> // { id: number, name: string }
1919
*/
20-
export type ChildItemType<T, key extends keyof T> = T[key] extends Array<any> ? T[key][number] : T[key];
21-
22-
20+
export type ChildItemType<T, key extends keyof T> =
21+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
22+
T[key] extends Array<any> ? T[key][number] : T[key];
2323

2424
/**
2525
* The type of the child.
@@ -38,4 +38,3 @@ export type ChildItemType<T, key extends keyof T> = T[key] extends Array<any> ?
3838
* type BarItem = ChildType<Obj, "bar"> // { baz: number }
3939
*/
4040
export type ChildType<T, key extends keyof T> = T[key];
41-

0 commit comments

Comments
 (0)