From ee597ce77eca580b2699a891ea8ef5d74ff86403 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Sat, 1 Dec 2018 11:03:48 +0100 Subject: [PATCH] Add types for jsonfile v5, fix v4 --- types/jsonfile/index.d.ts | 71 ++++++++++++++++++++++++++++++++ types/jsonfile/jsonfile-tests.ts | 67 ++++++++++++++++++++++++++++++ types/jsonfile/tsconfig.json | 23 +++++++++++ types/jsonfile/tslint.json | 1 + types/jsonfile/v4/tsconfig.json | 11 +++-- 5 files changed, 170 insertions(+), 3 deletions(-) create mode 100644 types/jsonfile/index.d.ts create mode 100644 types/jsonfile/jsonfile-tests.ts create mode 100644 types/jsonfile/tsconfig.json create mode 100644 types/jsonfile/tslint.json diff --git a/types/jsonfile/index.d.ts b/types/jsonfile/index.d.ts new file mode 100644 index 0000000000..cc0009ece3 --- /dev/null +++ b/types/jsonfile/index.d.ts @@ -0,0 +1,71 @@ +// Type definitions for jsonfile 5.0 +// Project: https://github.com/jprichardson/node-jsonfile#readme +// Definitions by: Daniel Bowring +// BendingBender +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 + +/// + +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; + +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; + +export function writeFileSync(file: Path, obj: any, options?: JFWriteOptions): void; diff --git a/types/jsonfile/jsonfile-tests.ts b/types/jsonfile/jsonfile-tests.ts new file mode 100644 index 0000000000..4725e3c8a3 --- /dev/null +++ b/types/jsonfile/jsonfile-tests.ts @@ -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 +jsonfile.writeFile(file, obj); +// $ExpectType Promise +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' }); diff --git a/types/jsonfile/tsconfig.json b/types/jsonfile/tsconfig.json new file mode 100644 index 0000000000..17ac9b0896 --- /dev/null +++ b/types/jsonfile/tsconfig.json @@ -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" + ] +} \ No newline at end of file diff --git a/types/jsonfile/tslint.json b/types/jsonfile/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/jsonfile/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/jsonfile/v4/tsconfig.json b/types/jsonfile/v4/tsconfig.json index 17ac9b0896..e6505d789e 100644 --- a/types/jsonfile/v4/tsconfig.json +++ b/types/jsonfile/v4/tsconfig.json @@ -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" ] -} \ No newline at end of file +}