diff --git a/types/iferr/iferr-tests.ts b/types/iferr/iferr-tests.ts new file mode 100644 index 0000000000..f7377358cf --- /dev/null +++ b/types/iferr/iferr-tests.ts @@ -0,0 +1,38 @@ +/// + +import * as fs from "fs"; +import iferr = require("iferr"); + +const { tiferr, throwerr, printerr } = iferr; + +const { readFile } = fs; + +const errcb = (err: Error | undefined) => { throw err; }; + +readFile("test.txt", iferr(errcb, result => { + Buffer.from("test", "utf8") === result; +})); + +readFile("test.txt", "utf8", iferr(errcb, result => { + "test" === result; +})); + +readFile("test.txt", tiferr(errcb, result => { + Buffer.from("test", "utf8") === result; +})); + +readFile("test.txt", "utf8", tiferr(errcb, result => { + "test" === result; +})); + +readFile("test.txt", throwerr(result => { + Buffer.from("test", "utf8") === result; +})); + +readFile("test.txt", "utf8", throwerr(result => { + "test" === result; +})); + +readFile("test.txt", printerr); + +readFile("test.txt", "utf8", printerr); diff --git a/types/iferr/index.d.ts b/types/iferr/index.d.ts new file mode 100644 index 0000000000..c65c270b49 --- /dev/null +++ b/types/iferr/index.d.ts @@ -0,0 +1,29 @@ +// Type definitions for iferr 1.0 +// Project: https://github.com/shesek/iferr +// Definitions by: segayuu +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +type nodeCallback = (err: Error | undefined, ...a: T[]) => any; + +// Delegates to `succ` on sucecss or to `fail` on error +// ex: Thing.load(123, iferr(cb, thing => ...)) +declare function iferr(fail: (err: Error | undefined) => void, succ: (...result: T[]) => void): nodeCallback; + +declare namespace iferr { + // Delegates to `succ` on sucecss or to `fail` on error + // ex: Thing.load(123, iferr(cb, thing => ...)) + function iferr(fail: (err: Error | undefined) => void, succ: (...result: T[]) => void): nodeCallback; + + // Like iferr, but also catches errors thrown from `succ` and passes to `fail` + function tiferr(fail: (err: Error | undefined) => void, succ: (...result: T[]) => void): nodeCallback; + + // Delegate to the success function on success, throws the error otherwise + // ex: Thing.load(123, throwerr(thing => ...)) + function throwerr(succ: (...result: T[]) => void): nodeCallback; + + // Prints errors when one is passed, or does nothing otherwise + // ex: Thing.load(123, printerr) + function printerr(): nodeCallback; +} + +export = iferr; diff --git a/types/iferr/tsconfig.json b/types/iferr/tsconfig.json new file mode 100644 index 0000000000..db97bdf495 --- /dev/null +++ b/types/iferr/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", + "iferr-tests.ts" + ] +} diff --git a/types/iferr/tslint.json b/types/iferr/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/iferr/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }