Skip to content

Better typing for throws assertions #1893

@novemberborn

Description

@novemberborn

This is the last piece of work that's come out of the discussion in #1794.

The t.throws() and t.throwsAsync() assertions are currently typed to return any and Promise<any> respectively. This is because they only return an error if one was actually thrown. If the assertion fails they return undefined and execution continues.

Whilst theoretically correct, we don't feel it's the pragmatic choice. Instead we should type these assertions to return Error / Promise<Error>. Additionally it'd be great if the assertions could be typed as generics so you can provide your own Error subclass (const err = t.throws<MyError>(fn) or const err: MyError = t.throws(fn)). Note that the assertion requires that an Error instance is thrown, so the generic shouldn't take any value.

This should come with some example code in test/flow-types and test/ts-types, and be documented in our Flow and TypeScript recipes.

We can land implementations for Flow and TypeScript separately, though keep in mind that we like to keep diffs between the index.d.ts and index.js.flow files to a minimum.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions