diff --git a/types/err-code/err-code-tests.ts b/types/err-code/err-code-tests.ts new file mode 100644 index 0000000000..3897a151f6 --- /dev/null +++ b/types/err-code/err-code-tests.ts @@ -0,0 +1,55 @@ +import errCode = require('err-code'); + +// test create without code +const err1 = errCode(new Error('err msg'), { + prop1: 'prop1 value', + prop2: { + a: 'prop2.a value', + } +}); + +const err1Message = err1.message; +const err1MProp1 = err1.prop1; +const err1MProp2a = err1.prop2.a; + +// test create with code +const err2 = errCode(new Error('err msg'), 'Bad Request', { + prop1: 'prop1 value', + prop2: { + a: 'prop2.a value', + } +}); + +const err2Message = err2.message; +const err2MProp1 = err2.prop1; +const err2MProp2a = err2.prop2.a; +const err2Code = err2.code; + +// test adding type constraint +const err3 = errCode<{ + prop1: string + prop2: { + a: string + } +}>(new Error('err msg'), { + prop1: 'prop1 value', + prop2: { + a: 'prop2.a value', + } +}); + +const err3Message = err3.message; +const err3MProp1 = err3.prop1; +const err3MProp2a = err3.prop2.a; + +// test null code +const err4 = errCode(new Error('err msg'), null, { + prop1: 'prop1 value', + prop2: { + a: 'prop2.a value', + } +}); + +const err4Message = err4.message; +const err4MProp1 = err4.prop1; +const err4MProp2a = err4.prop2.a; diff --git a/types/err-code/index.d.ts b/types/err-code/index.d.ts new file mode 100644 index 0000000000..a26520cd4a --- /dev/null +++ b/types/err-code/index.d.ts @@ -0,0 +1,24 @@ +// Type definitions for err-code 2.0 +// Project: https://github.com/IndigoUnited/js-err-code#readme +// Definitions by: George +// Cayman +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// Minimum TypeScript Version: 3.2 + +type Scalar = number | string | boolean | bigint | null | undefined; + +declare function createError( + error: Err, + props?: Props, +): Err & Props; + +declare function createError( + error: Err, + code: Code, + props?: Props, +): Err & Props & { code: NonNullable }; + +declare namespace createError { +} + +export = createError; diff --git a/types/err-code/tsconfig.json b/types/err-code/tsconfig.json new file mode 100644 index 0000000000..5bd9815b43 --- /dev/null +++ b/types/err-code/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "err-code-tests.ts" + ] +} diff --git a/types/err-code/tslint.json b/types/err-code/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/err-code/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }