Skip to content

Conversation

@SethFalco
Copy link
Member

@SethFalco SethFalco commented Oct 11, 2025

This defines clearer types for SVGO presets.

Regular plugins do not have the isPreset or plugins property defined. Whereas, presets always have both isPreset and plugins defined.

Before, we simply marked both as optional properties of a preset. The problem is that this prompted developers to do unnecessary checks, for example:

- if (plugin.isPreset && plugin.plugins) {
-   // Checking plugins shouldn't be neccesary. Presets always have a plugins array.
- }
+ if (plugin.isPreset) {
+   console.log(plugin.plugins); // We want to allow this.
+ }

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#isPreset and it returns true, then it will type-narrow the plugin to one of the presets.

For example:

const presetDefault = builtinPlugins.find((plugin) => plugin.name === 'preset-default')!;
if (!presetDefault.isPreset) {
  throw Error('Could not find preset-default.');
}

expectType<ReadonlyArray<BuiltinPlugin<string, Object>>>(presetDefault.plugins);
expectType<'preset-default'>(presetDefault.name);

It can now type-narrow the plugin to preset-default because we checked if it's a preset, and preset-default is 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:

  • A single type so that documentation only had to be included once. ✅
  • For the name, isPreset, and plugins property to type narrow in sync. ✅
  • Use optional properties rather than hard-coding 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. 👍

@SethFalco SethFalco merged commit ef7fe65 into svg:main Oct 11, 2025
14 checks passed
@SethFalco SethFalco deleted the preset-types branch October 11, 2025 23:00
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