fix: improve jsdoc types and remove excludes #2107
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is part 1 of what will be a series of pull requests to improve our typechecking with tsc.
This is proving important as some users have reported issues with type declarations. Before I tackle the issue directly, I want to improve our TypeScript setup. This will help in improving our QA for SVGO too, which is a long-term project I'd been focused on since joining as a maintainer.
Note: This is not an effort to migrate the project to TypeScript, and I do not have plans to migrate the project to TypeScript. This is to improve the JSDoc types and our usage of tsc.
This does leave a few instances of
{any}
in, which I'll resolve in future. My goal is better, not perfect, and I'll iterate on this in other PRs.Upgrades TypeScript Version
Upgrades the version of typescript used in the project as there was a bug where it wasn't type narrowing in
plugins/inlineStyles.js
correctly. Simply upgrading the version resolved this.This allows us to drop one of the
@ts-expect-error
statements.Types
lib/parser.js
../svgo
→./svgo.js
)Implementation Changes
None of these should have any meaningful impact on the functionality of SVGO. They were just changes of convenience while trying to resolve all problems reported by tsc.
#import
accepts a string, not a URL. We usepath#resolve
instead ofurl#pathToFileURL
when converting a relative path to absolute.coa.js
, the assignment toopts
andconfig
were handled separately. I put these together to avoid having to declare a type union foropt
, which would have a cascading affect later when it's referenced.[].concat(arr1, arr2)
witharr1.concat(arr2)
as this simplified some typings, and is the equivalent, as#concat
does not modify either of the original arrays.Tests
try/catch
for the equivalent assertions that don't require atry/catch
.Bugs
lib/svgo/css-select-adapter.js
where we had an impossible predicate in an if condition. I'm surprised this didn't come up sooner, but it was clearly meant to be||
, not&&
.Addendum
In case the topic comes up in future. The reason we're not migrating to TypeScript files is that we can already get most of the benefits of it with JSDoc types and running the type checker over JavaScript files.
Apparently it's a controversial opinion, but I/we aren't the only ones that feel this way:
https://www.youtube.com/watch?v=5ChkQKUzDCs