Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript type for all id's in the generated sprite #731

Open
AlexisWalravens opened this issue Sep 19, 2022 · 2 comments
Open

Typescript type for all id's in the generated sprite #731

AlexisWalravens opened this issue Sep 19, 2022 · 2 comments

Comments

@AlexisWalravens
Copy link

Hi 馃憢,

I recently started using this project, wich is great to build sprite with no hassle!

I don't have any issue, it's more of an idea.

In my project I use React and Typescript.

I created a Icon component which takes a name prop which should be the id of the svg in the sprite I want to use.

interface IconProps {
  name: string
}

const Icon = ({ name }: IconProps) => {
  return (
    <svg>
      <use href={`icons.svg#${name}`} />
    </svg>
  )
}

And it works great, but miss one thing. Using typescript it would be amazing to be able to generate a type declaration that would contain all of the id of the svg in the sprite so I would get autocompletion for my name prop and I would be able to create my interface like this:

interface IconProps {
  name: IconIdInSprite
}

What do you think ?

@Kreeg
Copy link
Member

Kreeg commented Nov 7, 2022

Hello. The thing is that all icons and icons names are generated in runtime. On the other hand, TS is an superset of JS, the type checking takes place on compilation time.
You can store somewhere set of all svg names, get enum from that (IconIdInSprite), then manually add them to sprite
https://github.com/svg-sprite/svg-sprite/blob/main/docs/api.md#svgspriteraddfile--name-svg-
https://github.com/svg-sprite/svg-sprite/blob/main/docs/api.md#svgspritercompile-config--callback-

@chimok
Copy link

chimok commented Oct 6, 2023

@Kreeg But I think what he wants is generating the interface at runtime, so he will have autocomplete inside the ide. That should be quite easy. @AlexisWalravens This package does it but doesn't have all the features of svg-sprite https://github.com/kreuzerk/svg-to-ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants