Skip to content

Commit cb6be81

Browse files
committed
fix: readJsonFile/writeJsonFile types
1 parent 5e5aba1 commit cb6be81

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/fs/readJsonFile.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@ import fs from 'node:fs'
33
import { deepMerge } from '../object/deepMerge';
44
import { promises as fsPromises } from 'node:fs';
55

6-
export function readJsonFile(jsonfile: string,defaultValue?:Record<string,any>) {
7-
let json = {}
6+
export function readJsonFile<T extends Record<string,any> = Record<string,any>>(jsonfile: string,defaultValue?:Partial<T>):T | undefined {
7+
let json:T = {} as T
88
if(fs.existsSync(jsonfile)){
99
json = JSON.parse(fs.readFileSync(jsonfile,{encoding:'utf-8'}).toString())
1010
}
1111
if(defaultValue){
12-
json = deepMerge({},defaultValue,json)
12+
json = deepMerge({},defaultValue,json as any) as T
1313
}
1414
return json
1515
}
1616

17-
export async function readJsonFileAsync(jsonfile: string, defaultValue?: Record<string, any>) {
18-
let json = {};
17+
export async function readJsonFileAsync<T extends Record<string,any>= Record<string,any>>(jsonfile: string, defaultValue?: Partial<T>):Promise<T | undefined> {
18+
let json = {} as T
1919
if (await fsPromises.access(jsonfile).then(() => true).catch(() => false)) {
2020
json = JSON.parse((await fsPromises.readFile(jsonfile, { encoding: 'utf-8' })).toString());
2121
}
2222
if (defaultValue) {
23-
json = deepMerge({}, defaultValue, json);
23+
json = deepMerge({}, defaultValue, json) as T
2424
}
2525
return json;
26-
}
26+
}

src/fs/writeJsonFile.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@ import fs from 'node:fs';
33
import { deepMerge } from '../object/deepMerge';
44
import { promises as fsPromises } from 'node:fs';
55

6-
export function writeJsonFile(jsonfile: string, data:Record<string,any>) {
7-
let json = {}
6+
export function writeJsonFile<T extends Record<string,any> = Record<string,any>>(jsonfile: string, data:T) {
7+
let json = {} as T
88
if(fs.existsSync(jsonfile)){
99
json = JSON.parse(fs.readFileSync(jsonfile,{encoding:'utf-8'}).toString())
1010
}
11-
json = deepMerge(json,data)
11+
json = deepMerge(json,data) as T
1212
fs.writeFileSync(jsonfile,JSON.stringify(json,null,4))
1313
return json
1414

1515
}
1616

17-
export async function writeJsonFileAsync(jsonfile: string, data: Record<string, any>) {
18-
let json = {};
17+
export async function writeJsonFileAsync<T extends Record<string,any> = Record<string,any>>(jsonfile: string, data: Record<string, any>):Promise<T | undefined> {
18+
let json = {} as T;
1919
if (await fsPromises.stat(jsonfile).then(() => true).catch(() => false)) {
2020
const fileContent = await fsPromises.readFile(jsonfile, { encoding: 'utf-8' });
2121
json = JSON.parse(fileContent.toString());
2222
}
23-
json = deepMerge(json, data);
23+
json = deepMerge(json, data) as T
2424
await fsPromises.writeFile(jsonfile, JSON.stringify(json, null, 4));
2525
return json;
2626
}

0 commit comments

Comments
 (0)