types: clearer types for presets #2178
Merged
+29
−9
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 defines clearer types for SVGO presets.
Regular plugins do not have the
isPresetorpluginsproperty defined. Whereas, presets always have bothisPresetandpluginsdefined.Before, we simply marked both as optional properties of a preset. The problem is that this prompted developers to do unnecessary checks, for example:
This example is based on a community contribution we received:
We'll mark these properties as optional for normal plugins, but mandatory for presets. This also adds the benefit that if one checks
BuiltinPluginOrPreset#isPresetand it returnstrue, then it will type-narrow the plugin to one of the presets.For example:
It can now type-narrow the plugin to
preset-defaultbecause we checked if it's a preset, andpreset-defaultis the only preset available.This also therefore introduces a rule where presets must be named
preset-whatever. For example,preset-default,preset-html5,preset-svg2, etc.Development Notes
What I really wanted:
name,isPreset, andpluginsproperty to type narrow in sync. ✅undefined. But, I had trouble achieving what I wanted here. ❌I may revisit this later. Alternatively, feedback is always welcome, even after this PR is merged. 👍