Skip to content

fix: improve jsdoc types and remove excludes #2107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2025

Conversation

SethFalco
Copy link
Member

@SethFalco SethFalco commented Apr 28, 2025

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

  • Corrects the JS Doc type syntax in lib/parser.js.
  • Appends the file extension to some import strings for files that were not typechecked before. (i.e. ./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 use path#resolve instead of url#pathToFileURL when converting a relative path to absolute.
  • In coa.js, the assignment to opts and config were handled separately. I put these together to avoid having to declare a type union for opt, which would have a cascading affect later when it's referenced.
  • Replaces some instances of [].concat(arr1, arr2) with arr1.concat(arr2) as this simplified some typings, and is the equivalent, as #concat does not modify either of the original arrays.

Tests

  • Replaces assertions with try/catch for the equivalent assertions that don't require a try/catch.

Bugs

  • Fixes a bug in 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

@SethFalco SethFalco merged commit 71a1254 into svg:main Apr 28, 2025
11 checks passed
@SethFalco SethFalco deleted the fix-jsdoc-types branch April 28, 2025 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant