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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Svelte and TS errors when importing svgs #25

Closed
plitarko opened this issue Nov 6, 2022 · 11 comments
Closed

Svelte and TS errors when importing svgs #25

plitarko opened this issue Nov 6, 2022 · 11 comments

Comments

@plitarko
Copy link

plitarko commented Nov 6, 2022

If I use ?component I get this:
Screenshot 2022-11-06 at 14 58 12

If I remove ?component I get this:
Screenshot 2022-11-06 at 14 58 51

Also had to import the vite Plugin type to make this plugin work. Everything works now despite these errors, but how can I fix them? Thanks for you help!

@poppa
Copy link
Owner

poppa commented Nov 6, 2022

I have no idea of your setup, but try giving this a try. Change the *.svg?component-typing to this:

declare module '*.svg?component' {
  const content: ConstructorOfATypedSvelteComponent
  export default content
}

Let me know if that helps?

poppa added a commit that referenced this issue Nov 7, 2022
This most likely, partly, solves #25

Also give better suggestions for type defs with proper Svelte typed component def
@poppa
Copy link
Owner

poppa commented Nov 7, 2022

I've made a new release where I think this issue may be solved.

Feel free to re-open this issue if the problem persists.

@poppa poppa closed this as completed Nov 7, 2022
@tcherokee
Copy link

tcherokee commented Nov 15, 2022

Hello,

I am having a similar issue to this. I have added

declare module '*.svg?component' {
	const content: ConstructorOfATypedSvelteComponent
	export default content
}

but I am still getting an error with it
Screenshot 2022-11-15 at 13 00 44

I am not sure what I am missing?

@poppa
Copy link
Owner

poppa commented Nov 15, 2022

Hi @tcherokee. Perhaps you'r on an old version of SvelteKit? Either upgrade or change the type to any.

Hope that helps.

@coldino
Copy link

coldino commented Mar 20, 2023

I'm not sure why, but putting the declare module '*.svg?component' section in app.d.ts isn't working for me as I still get Cannot find module with an import like $lib/images/dev.svg?component. I have other things in the app.d.ts that DO work as expected - for example a session added to App.Locals.

Experimenting a little, I found putting it in a .d.ts beside the .svelte file works. In my case this is my global +layout.svelte and the declaration works if it lives in +layout.d.ts.

Of course this is unlikely to be caused by an issue in this library, but does anyone know the cause, and do the docs here need to be updated?

@poppa
Copy link
Owner

poppa commented Mar 25, 2023

Tanks for the message @coldino, very much appreciated.

In my personal opinion there are too many moving parts when it comes to a project and how TS and type-resolution etc is setup, that it feels more or less like a guessing-game getting all these things to work 😂

Now, if anyone makes a PR with more sensible defaults, I'll be the first to merge it ☺️

@miguelcobain
Copy link

Also looking for solutions. @coldino your workaround does work, indeed.

poppa added a commit that referenced this issue May 20, 2023
It seems Svelte doesn't like this being in app.d.ts, and I've found there's no problem having in as svg.d.ts.

See #25 for reference
@woss
Copy link

woss commented Jul 8, 2023

i created the ./src/ambient.d.ts file then added the declare stuff inside and it worked without any issues. Having it in the app.d.ts doesn't work, I confirm.

@poppa poppa pinned this issue Jul 8, 2023
@Porter-smith
Copy link

Porter-smith commented Dec 14, 2023

i installed with bun add --dev @poppanator/sveltekit-svg and got this issue. I think maybe bun doesn't add the typing (idk). I just added the type to app.d.tsand that worked for me

declare module '*.svg?component' {
  const content: ConstructorOfATypedSvelteComponent
  export default content
}

@poppa
Copy link
Owner

poppa commented Dec 14, 2023

i installed with bun add --dev @poppanator/sveltekit-svg and got this issue. I think maybe bun doesn't add the typing (idk). I just added the type to app.d.tsand that worked for me

declare module '*.svg?component' {
  const content: ConstructorOfATypedSvelteComponent
  export default content
}

@Porter-smith Did you try this? https://github.com/poppa/sveltekit-svg/#typescript

@Porter-smith
Copy link

Oh I see ! I didn't realize it had that thank you !

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

No branches or pull requests

7 participants