mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
[check-error] Add type definitions. (#38778)
This commit is contained in:
parent
4f4381c06b
commit
8aa928a778
71
types/check-error/check-error-tests.ts
Normal file
71
types/check-error/check-error-tests.ts
Normal file
@ -0,0 +1,71 @@
|
||||
import * as checkError from 'check-error';
|
||||
|
||||
const errorInstance = new Error('I am an instance');
|
||||
const sameInstance = errorInstance;
|
||||
const otherInstance = new Error('I an another instance');
|
||||
const derivedInstance = new TypeError('I inherit from Error');
|
||||
const thrownMessage = 'Imagine I have been thrown';
|
||||
|
||||
class CustomError extends Error {
|
||||
constructor(message?: string) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
const customError = new CustomError();
|
||||
|
||||
// compatibleInstance()
|
||||
// $ExpectType boolean
|
||||
checkError.compatibleInstance(errorInstance, sameInstance);
|
||||
checkError.compatibleInstance(errorInstance, otherInstance);
|
||||
|
||||
checkError.compatibleInstance(customError, errorInstance);
|
||||
checkError.compatibleInstance(errorInstance, customError);
|
||||
|
||||
// compatibleConstructor()
|
||||
// $ExpectType boolean
|
||||
checkError.compatibleConstructor(errorInstance, sameInstance);
|
||||
checkError.compatibleConstructor(errorInstance, otherInstance);
|
||||
checkError.compatibleConstructor(derivedInstance, errorInstance);
|
||||
checkError.compatibleConstructor(errorInstance, derivedInstance);
|
||||
|
||||
checkError.compatibleConstructor(errorInstance, Error);
|
||||
checkError.compatibleConstructor(derivedInstance, TypeError);
|
||||
checkError.compatibleConstructor(errorInstance, TypeError);
|
||||
|
||||
checkError.compatibleConstructor(customError, errorInstance);
|
||||
checkError.compatibleConstructor(errorInstance, customError);
|
||||
|
||||
// compatibleMessage()
|
||||
// $ExpectType boolean
|
||||
checkError.compatibleMessage(errorInstance, /instance$/);
|
||||
checkError.compatibleMessage(derivedInstance, /Error$/);
|
||||
checkError.compatibleMessage(errorInstance, /unicorn$/);
|
||||
checkError.compatibleMessage(derivedInstance, /dinosaur$/);
|
||||
checkError.compatibleMessage(customError, /dinosaur$/);
|
||||
|
||||
checkError.compatibleMessage(errorInstance, 'instance');
|
||||
checkError.compatibleMessage(derivedInstance, 'Error');
|
||||
checkError.compatibleMessage(errorInstance, 'unicorn');
|
||||
checkError.compatibleMessage(derivedInstance, 'dinosaur');
|
||||
checkError.compatibleMessage(customError, 'def');
|
||||
|
||||
// constructorName()
|
||||
// $ExpectType string
|
||||
checkError.getConstructorName(errorInstance);
|
||||
checkError.getConstructorName(derivedInstance);
|
||||
|
||||
checkError.getConstructorName(Error);
|
||||
checkError.getConstructorName(TypeError);
|
||||
|
||||
checkError.getConstructorName(Error);
|
||||
checkError.getConstructorName(TypeError);
|
||||
checkError.getConstructorName(CustomError);
|
||||
checkError.getConstructorName(customError);
|
||||
|
||||
// getMessage()
|
||||
// $ExpectType string
|
||||
checkError.getMessage(errorInstance);
|
||||
checkError.getMessage(derivedInstance);
|
||||
|
||||
checkError.getMessage(thrownMessage);
|
||||
66
types/check-error/index.d.ts
vendored
Normal file
66
types/check-error/index.d.ts
vendored
Normal file
@ -0,0 +1,66 @@
|
||||
// Type definitions for check-error 1.0
|
||||
// Project: https://github.com/chaijs/check-error#readme
|
||||
// Definitions by: Porama Ignoi Ruengrairatanaroj <https://github.com/Seally>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.1
|
||||
|
||||
export type ErrorInstanceOrConstructor =
|
||||
| Error
|
||||
| {
|
||||
new (...args: any[]): Error;
|
||||
}
|
||||
| {
|
||||
(...args: any[]): Error;
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if two instances are compatible (strict equal).
|
||||
*
|
||||
* Returns false if errorLike is not an instance of Error, because instances
|
||||
* can only be compatible if they're both error instances.
|
||||
*
|
||||
* @param thrown - thrown error
|
||||
* @param errorLike - error object to compare against
|
||||
*/
|
||||
export function compatibleInstance(thrown: Error, errorLike: Error): boolean;
|
||||
|
||||
/**
|
||||
* Checks if two constructors are compatible.
|
||||
*
|
||||
* This function can receive either an error constructor or an error
|
||||
* instance as the `errorLike` argument.
|
||||
*
|
||||
* Constructors are compatible if they're the same or if one is an instance
|
||||
* of another.
|
||||
*
|
||||
* @param thrown - error
|
||||
* @param errorLike - object to compare against
|
||||
*/
|
||||
export function compatibleConstructor(thrown: Error, errorLike: ErrorInstanceOrConstructor): boolean;
|
||||
|
||||
/**
|
||||
* Checks if an error's message is compatible with a matcher (String or
|
||||
* RegExp).
|
||||
*
|
||||
* If the message contains the String or passes the RegExp test,
|
||||
* it is considered compatible.
|
||||
*
|
||||
* @param thrown - thrown error
|
||||
* @param errMatcher - string/regex to look for in the message
|
||||
*/
|
||||
export function compatibleMessage(thrown: Error | string, errMatcher: string | RegExp): boolean;
|
||||
|
||||
/**
|
||||
* Gets the error message from an error.
|
||||
*
|
||||
* If `err` is a String itself, we return it.
|
||||
* If the error has no message, we return an empty string.
|
||||
*
|
||||
* @param err
|
||||
*/
|
||||
export function getMessage(err: Error | string): string;
|
||||
|
||||
/**
|
||||
* Gets the constructor name for an Error instance or constructor itself.
|
||||
*/
|
||||
export function getConstructorName(errorLike: ErrorInstanceOrConstructor): string;
|
||||
23
types/check-error/tsconfig.json
Normal file
23
types/check-error/tsconfig.json
Normal file
@ -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",
|
||||
"check-error-tests.ts"
|
||||
]
|
||||
}
|
||||
1
types/check-error/tslint.json
Normal file
1
types/check-error/tslint.json
Normal file
@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Loading…
Reference in New Issue
Block a user