Skip to content

Commit

Permalink
Improve types and type coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed Nov 9, 2021
1 parent 481d3a0 commit 8eb3d69
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 6 deletions.
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

/** @template T */
/** @template [T=undefined] */
class ErrorWithCause extends Error {
/**
* @param {string} message
Expand All @@ -9,8 +9,13 @@ class ErrorWithCause extends Error {
constructor (message, { cause } = {}) {
super(message);

/** @type {string} */
this.name = ErrorWithCause.name;
this.cause = cause;
if (cause) {
/** @type {T} */
this.cause = cause;
}
/** @type {string} */
this.message = message;
}
}
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"check:installed-check": "installed-check -i dependency-check -i installed-check",
"check:lint": "eslint .",
"check:tsc": "tsc",
"check:type-coverage": "type-coverage --detail --strict --at-least 97 --ignore-files 'test/*'",
"check": "run-s clean && run-p check:*",
"clean:declarations": "rm -rf $(find . -maxdepth 2 -type f -name '*.d.ts')",
"clean": "run-p clean:*",
Expand Down Expand Up @@ -50,7 +51,7 @@
"@types/verror": "^1.10.5",
"@voxpelli/eslint-config": "^12.0.2",
"@voxpelli/eslint-config-jsdoc-ts": "^0.3.1",
"@voxpelli/tsconfig": "^3.0.0",
"@voxpelli/tsconfig": "^3.1.0-0",
"c8": "^7.10.0",
"chai": "^4.3.4",
"chai-string": "^1.5.0",
Expand All @@ -70,7 +71,8 @@
"installed-check": "^5.0.0-1",
"mocha": "^9.1.3",
"npm-run-all": "^4.1.5",
"typescript": "~4.4.4",
"type-coverage": "^2.19.0",
"typescript": "^4.5.1-rc",
"verror": "^1.10.1"
}
}
4 changes: 2 additions & 2 deletions test/error.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ describe('ErrorWithCause', () => {
it('should handle missing options object', () => {
const err = new ErrorWithCause('Foo');

err.should.have.property('cause', undefined);
err.should.not.have.property('cause');
err.should.have.property('message', 'Foo');
});

it('should handle empty options object', () => {
(new ErrorWithCause('Foo', {})).should.have.property('cause', undefined);
(new ErrorWithCause('Foo', {})).should.not.have.property('cause');
});

it('should produce a proper stack trace', () => {
Expand Down
1 change: 1 addition & 0 deletions test/find.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ describe('findCauseByReference()', () => {
const cause = new ErrorWithCause('Foo');
const err = new ErrorWithCause('Bar', { cause });

// @ts-ignore
cause.cause = err;

const result = findCauseByReference(err, SubError);
Expand Down
1 change: 1 addition & 0 deletions test/message.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ describe('messageWithCauses()', () => {
const cause = new ErrorWithCause('Foo');
const err = new ErrorWithCause('Bar', { cause });

// @ts-ignore
cause.cause = err;

const result = messageWithCauses(err);
Expand Down
1 change: 1 addition & 0 deletions test/stack.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ describe('stackWithCauses()', () => {
cause.stack = 'abc123';
err.stack = 'xyz789';

// @ts-ignore
cause.cause = err;

const result = stackWithCauses(err);
Expand Down

1 comment on commit 8eb3d69

@voxpelli
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussion on some of these changes: voxpelli/types-in-js#17

Please sign in to comment.