From 2f615010a5dfec300363b746c63db18f5e4e076f Mon Sep 17 00:00:00 2001 From: Florian Imdahl Date: Fri, 28 Aug 2020 11:38:47 +0200 Subject: [PATCH] [whoops] Add types for whoops (#47000) --- types/whoops/index.d.ts | 26 ++++++++++++++++++++++++++ types/whoops/tsconfig.json | 23 +++++++++++++++++++++++ types/whoops/tslint.json | 1 + types/whoops/whoops-tests.ts | 20 ++++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 types/whoops/index.d.ts create mode 100644 types/whoops/tsconfig.json create mode 100644 types/whoops/tslint.json create mode 100644 types/whoops/whoops-tests.ts diff --git a/types/whoops/index.d.ts b/types/whoops/index.d.ts new file mode 100644 index 0000000000..9b4861caef --- /dev/null +++ b/types/whoops/index.d.ts @@ -0,0 +1,26 @@ +// Type definitions for whoops 4.1 +// Project: https://github.com/Kikobeats/whoops +// Definitions by: Florian Imdahl +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.9 + +declare namespace createErrorClass { + type ExtendError = any> = (propsOrMessage?: P | string) => E & P; + + function type(className?: string, props?: Record): ExtendError; + function range(className?: string, props?: Record): ExtendError; + // function eval(className?: string, props?: Record): ExtendError; + function syntax(className?: string, props?: Record): ExtendError; + function reference(className?: string, props?: Record): ExtendError; + function uri(className?: string, props?: Record): ExtendError; +} + +declare function createErrorClass(className?: string, props?: Record): createErrorClass.ExtendError; +declare function createErrorClass(className?: 'TypeError', props?: Record): createErrorClass.ExtendError; +declare function createErrorClass(className?: 'RangeError', props?: Record): createErrorClass.ExtendError; +declare function createErrorClass(className?: 'EvalError', props?: Record): createErrorClass.ExtendError; +declare function createErrorClass(className?: 'SyntaxError', props?: Record): createErrorClass.ExtendError; +declare function createErrorClass(className?: 'ReferenceError', props?: Record): createErrorClass.ExtendError; +declare function createErrorClass(className?: 'URIError', props?: Record): createErrorClass.ExtendError; + +export = createErrorClass; diff --git a/types/whoops/tsconfig.json b/types/whoops/tsconfig.json new file mode 100644 index 0000000000..51c62f064c --- /dev/null +++ b/types/whoops/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", + "whoops-tests.ts" + ] +} diff --git a/types/whoops/tslint.json b/types/whoops/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/whoops/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/whoops/whoops-tests.ts b/types/whoops/whoops-tests.ts new file mode 100644 index 0000000000..2c094e6946 --- /dev/null +++ b/types/whoops/whoops-tests.ts @@ -0,0 +1,20 @@ +import whoops = require('whoops'); + +const simpleUserError = whoops('UserError'); +throw simpleUserError('User not found'); + +const basicError = whoops(); +throw basicError(); + +const userError2 = whoops('userError'); +throw userError2(); + +const extendedError = whoops('userError', { code: 'ENOVALID' }); +extendedError().code; + +const extendedUserError = whoops('userError', { + code: 'ENOVALID', + message: (props: any) => `User '${props.username}' not found`, +}); + +extendedUserError({ username: 'kiko' });