Merge pull request #25925 from segayuu/create-types-iferr

Create @types/iferr
This commit is contained in:
Daniel Rosenwasser 2018-05-21 21:54:23 -07:00 committed by GitHub
commit 08e6ec3c77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 91 additions and 0 deletions

View File

@ -0,0 +1,38 @@
/// <reference types="node" />
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);

29
types/iferr/index.d.ts vendored Normal file
View File

@ -0,0 +1,29 @@
// Type definitions for iferr 1.0
// Project: https://github.com/shesek/iferr
// Definitions by: segayuu <https://github.com/segayuu>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
type nodeCallback<T> = (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<T>(fail: (err: Error | undefined) => void, succ: (...result: T[]) => void): nodeCallback<T>;
declare namespace iferr {
// Delegates to `succ` on sucecss or to `fail` on error
// ex: Thing.load(123, iferr(cb, thing => ...))
function iferr<T>(fail: (err: Error | undefined) => void, succ: (...result: T[]) => void): nodeCallback<T>;
// Like iferr, but also catches errors thrown from `succ` and passes to `fail`
function tiferr<T>(fail: (err: Error | undefined) => void, succ: (...result: T[]) => void): nodeCallback<T>;
// Delegate to the success function on success, throws the error otherwise
// ex: Thing.load(123, throwerr(thing => ...))
function throwerr<T>(succ: (...result: T[]) => void): nodeCallback<T>;
// Prints errors when one is passed, or does nothing otherwise
// ex: Thing.load(123, printerr)
function printerr(): nodeCallback<any>;
}
export = iferr;

23
types/iferr/tsconfig.json Normal file
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",
"iferr-tests.ts"
]
}

1
types/iferr/tslint.json Normal file
View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }