1
- export type ErrorConstructor = new ( ...args : any [ ] ) => Error ;
1
+ export type ErrorConstructor < ErrorType extends Error = Error > = {
2
+ new ( ...args : any [ ] ) : ErrorType ;
3
+ readonly prototype : ErrorType ;
4
+ }
5
+
6
+ export type ThrownError < ErrorType extends ErrorConstructor | Error > = ErrorType extends ErrorConstructor ? ErrorType [ 'prototype' ] : ErrorType ;
2
7
3
8
/** Specify one or more expectations the thrown error must satisfy. */
4
- export type ThrowsExpectation = {
9
+ export type ThrowsExpectation < ErrorType extends ErrorConstructor | Error > = {
5
10
/** The thrown error must have a code that equals the given string or number. */
6
11
code ?: string | number ;
7
12
8
13
/** The thrown error must be an instance of this constructor. */
9
- instanceOf ?: ErrorConstructor ;
14
+ instanceOf ?: ErrorType extends ErrorConstructor ? ErrorType : ErrorType extends Error ? ErrorConstructor < ErrorType > : never ;
10
15
11
16
/** The thrown error must be strictly equal to this value. */
12
- is ?: Error ;
17
+ is ?: ErrorType extends ErrorConstructor ? ErrorType [ 'prototype' ] : ErrorType ;
13
18
14
19
/** The thrown error must have a message that equals the given string, or matches the regular expression. */
15
20
message ?: string | RegExp | ( ( message : string ) => boolean ) ;
@@ -293,7 +298,7 @@ export type ThrowsAssertion = {
293
298
* Assert that the function throws [an error](https://www.npmjs.com/package/is-error). If so, returns the error value.
294
299
* The error must satisfy all expectations. Returns undefined when the assertion fails.
295
300
*/
296
- < ThrownError extends Error > ( fn : ( ) => any , expectations ?: ThrowsExpectation , message ?: string ) : ThrownError | undefined ;
301
+ < ErrorType extends ErrorConstructor | Error > ( fn : ( ) => any , expectations ?: ThrowsExpectation < ErrorType > , message ?: string ) : ThrownError < ErrorType > | undefined ;
297
302
298
303
/** Skip this assertion. */
299
304
skip ( fn : ( ) => any , expectations ?: any , message ?: string ) : void ;
@@ -304,14 +309,14 @@ export type ThrowsAsyncAssertion = {
304
309
* Assert that the async function throws [an error](https://www.npmjs.com/package/is-error). If so, returns the error
305
310
* value. Returns undefined when the assertion fails. You must await the result. The error must satisfy all expectations.
306
311
*/
307
- < ThrownError extends Error > ( fn : ( ) => PromiseLike < any > , expectations ?: ThrowsExpectation , message ?: string ) : Promise < ThrownError | undefined > ;
312
+ < ErrorType extends ErrorConstructor | Error > ( fn : ( ) => PromiseLike < any > , expectations ?: ThrowsExpectation < ErrorType > , message ?: string ) : Promise < ThrownError < ErrorType > | undefined > ;
308
313
309
314
/**
310
315
* Assert that the promise rejects with [an error](https://www.npmjs.com/package/is-error). If so, returns the
311
316
* rejection reason. Returns undefined when the assertion fails. You must await the result. The error must satisfy all
312
317
* expectations.
313
318
*/
314
- < ThrownError extends Error > ( promise : PromiseLike < any > , expectations ?: ThrowsExpectation , message ?: string ) : Promise < ThrownError | undefined > ;
319
+ < ErrorType extends ErrorConstructor | Error > ( promise : PromiseLike < any > , expectations ?: ThrowsExpectation < ErrorType > , message ?: string ) : Promise < ThrownError < ErrorType > | undefined > ;
315
320
316
321
/** Skip this assertion. */
317
322
skip ( thrower : any , expectations ?: any , message ?: string ) : void ;
0 commit comments