Add types for jsonfile v5, fix v4

This commit is contained in:
Dimitri Benin
2018-12-01 11:03:48 +01:00
parent bd7721366d
commit ee597ce77e
5 changed files with 170 additions and 3 deletions
+71
View File
@@ -0,0 +1,71 @@
// Type definitions for jsonfile 5.0
// Project: https://github.com/jprichardson/node-jsonfile#readme
// Definitions by: Daniel Bowring <https://github.com/dbowring>
// BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
/// <reference types="node"/>
import { Url } from 'url';
import {
PathLike,
readFile as fsReadFile,
readFileSync as fsReadFileSync,
writeFile as fsWriteFile,
writeFileSync as fsWriteFileSync,
} from 'fs';
export type Path = PathLike | Url;
export interface FS {
readFile: typeof fsReadFile;
readFileSync: typeof fsReadFileSync;
writeFile: typeof fsWriteFile;
writeFileSync: typeof fsWriteFileSync;
}
export type JFReadOptions =
| {
encoding?: string | null;
flag?: string;
throws?: boolean;
fs?: FS;
reviver?: (key: any, value: any) => any;
}
| string
| null
| undefined;
export type JFWriteOptions =
| {
encoding?: string | null;
mode?: string | number;
flag?: string;
fs?: FS;
EOL?: string;
spaces?: string | number;
replacer?: (key: string, value: any) => any;
}
| string
| null;
export type ReadCallback = (err: NodeJS.ErrnoException | null, data: any) => void;
export type WriteCallback = (err: NodeJS.ErrnoException) => void;
export function readFile(file: Path, options: JFReadOptions, callback: ReadCallback): void;
export function readFile(file: Path, callback: ReadCallback): void;
export function readFile(file: Path, options?: JFReadOptions): Promise<any>;
export function readFileSync(file: Path, options?: JFReadOptions): any;
export function writeFile(
file: Path,
obj: any,
options: JFWriteOptions,
callback: WriteCallback
): void;
export function writeFile(file: Path, obj: any, callback: WriteCallback): void;
export function writeFile(file: Path, obj: any, options?: JFWriteOptions): Promise<void>;
export function writeFileSync(file: Path, obj: any, options?: JFWriteOptions): void;
+67
View File
@@ -0,0 +1,67 @@
import * as jsonfile from 'jsonfile';
const file = '/tmp/data.json';
const obj = { name: 'JP' };
// $ExpectType void
jsonfile.readFile(file, { encoding: 'utf8', throws: true }, (err, obj) => {
// $ExpectType ErrnoException | null
err;
// $ExpectType any
obj;
});
// $ExpectType void
jsonfile.readFile(file, (err, obj) => {
// $ExpectType ErrnoException | null
err;
// $ExpectType any
obj;
});
jsonfile.readFile(file).then(obj => {
// $ExpectType any
obj;
});
jsonfile.readFile(file, { encoding: 'utf8', throws: true }).then(obj => {
// $ExpectType any
obj;
});
// $ExpectType any
jsonfile.readFileSync(file);
jsonfile.readFileSync(file, { encoding: 'utf8', throws: true });
// $ExpectType void
jsonfile.writeFile(file, obj, err => {
// $ExpectType ErrnoException
err;
});
// $ExpectType void
jsonfile.writeFile(file, obj, { spaces: 2 }, err => {
// $ExpectType ErrnoException
err;
});
// $ExpectType void
jsonfile.writeFile(file, obj, { spaces: 2, EOL: '\r\n' }, err => {
// $ExpectType ErrnoException
err;
});
// $ExpectType void
jsonfile.writeFile(file, obj, { flag: 'a' }, err => {
// $ExpectType ErrnoException
err;
});
// $ExpectType Promise<void>
jsonfile.writeFile(file, obj);
// $ExpectType Promise<void>
jsonfile.writeFile(file, obj, { flag: 'a' });
jsonfile.writeFileSync(file, obj);
jsonfile.writeFileSync(file, obj, { spaces: 2 });
jsonfile.writeFileSync(file, obj, { spaces: 2, EOL: '\r\n' });
jsonfile.writeFileSync(file, obj, { flag: 'a' });
+23
View File
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"strictFunctionTypes": true
},
"files": [
"index.d.ts",
"jsonfile-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }
+8 -3
View File
@@ -7,10 +7,15 @@
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"baseUrl": "../../",
"typeRoots": [
"../"
"../../"
],
"paths": {
"jsonfile": [
"jsonfile/v4"
]
},
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
@@ -20,4 +25,4 @@
"index.d.ts",
"jsonfile-tests.ts"
]
}
}