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 ;
27
38/** Specify one or more expectations the thrown error must satisfy. */
4- export type ThrowsExpectation = {
9+ export type ThrowsExpectation < ErrorType extends ErrorConstructor | Error > = {
510 /** The thrown error must have a code that equals the given string or number. */
611 code ?: string | number ;
712
813 /** 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 ;
1015
1116 /** The thrown error must be strictly equal to this value. */
12- is ?: Error ;
17+ is ?: ErrorType extends ErrorConstructor ? ErrorType [ 'prototype' ] : ErrorType ;
1318
1419 /** The thrown error must have a message that equals the given string, or matches the regular expression. */
1520 message ?: string | RegExp | ( ( message : string ) => boolean ) ;
@@ -293,7 +298,7 @@ export type ThrowsAssertion = {
293298 * Assert that the function throws [an error](https://www.npmjs.com/package/is-error). If so, returns the error value.
294299 * The error must satisfy all expectations. Returns undefined when the assertion fails.
295300 */
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 ;
297302
298303 /** Skip this assertion. */
299304 skip ( fn : ( ) => any , expectations ?: any , message ?: string ) : void ;
@@ -304,14 +309,14 @@ export type ThrowsAsyncAssertion = {
304309 * Assert that the async function throws [an error](https://www.npmjs.com/package/is-error). If so, returns the error
305310 * value. Returns undefined when the assertion fails. You must await the result. The error must satisfy all expectations.
306311 */
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 > ;
308313
309314 /**
310315 * Assert that the promise rejects with [an error](https://www.npmjs.com/package/is-error). If so, returns the
311316 * rejection reason. Returns undefined when the assertion fails. You must await the result. The error must satisfy all
312317 * expectations.
313318 */
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 > ;
315320
316321 /** Skip this assertion. */
317322 skip ( thrower : any , expectations ?: any , message ?: string ) : void ;
0 commit comments