Skip to content

feat(icon): add type support for local SVG collection#360

Closed
CRBroughton wants to merge 2 commits intonuxt:mainfrom
CRBroughton:type-safe-local-icons
Closed

feat(icon): add type support for local SVG collection#360
CRBroughton wants to merge 2 commits intonuxt:mainfrom
CRBroughton:type-safe-local-icons

Conversation

@CRBroughton
Copy link
Copy Markdown

@CRBroughton CRBroughton commented Feb 19, 2025

🔗 Linked issue

resolves #359

❓ Type of change

  • 👌 Enhancement (improving an existing functionality)

📚 Description

This PR enhances the existing custom SVG collections feature by adding TypeScript type generation. Currently, when using custom SVG collections, developers don't get type safety or autocompletion for their icon names. This PR adds automatic type generation that creates TypeScript definitions for all SVGs in custom collections.

Current Behavior

<Icon name="custom:my-icon" /> // No type checking or autocompletion

New Behavior

<Icon name="custom:my-icon" /> // TypeScript enforces valid icon names and provides autocompletion

Example configuration:

// nuxt.config.ts
export default defineNuxtConfig({
  icon: {
    generateLocalSVGTypes: true, // Enable type generation (default: false)
    customCollections: {
      'my-icons': {
        prefix: 'navbar',
        dir: './assets/navbar'
      }
    }
  }
})

I've tested this with the included SVGs in the repository. Please let me know if you'd like me to make any adjustments; The only downside I can see right now is that if you have both local and non-local collections in use, you can't make use of the feature; There might be a way for us to widen the type if both local collections and other collections are in use at the same time.

Edit: Just reading through my own PR again; I think renaming the 'generateTypes' key to something like 'generateLocalSVGTypes' might be better for the user, as it's only going to work with local types, and the existing name might be misleading or confusing. Can change this tomorrow though if there's agreement.

Edit II: Changed the name to generateLocalSVGTypes

@antfu
Copy link
Copy Markdown
Member

antfu commented Jun 19, 2025

Thank you for your PR. But I feel this would be better to be handled on the IDE side, via something like https://github.com/antfu/vscode-iconify.

Injecting .d.ts in this case is a bit too fragile and not very reliable.

Thank you.

@antfu antfu closed this Jun 19, 2025
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.

Type safe local icon support

2 participants