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

feat!: add Svelte5 specific types #381

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

mattdavis90
Copy link

Description

When using the library with Svelte5 I noticed that I was getting errors when importing the icon components. This is because Svelte5 changed the type of components so I couldn't assign the icon components to a Component typed variable in typescript.

This PR updates the type exports to be in line with Svelte5 and removed all issues from my LSP and eslint.

Linked Issues

None that I could find

Additional context

None - happy to discuss further - I didn't open an issue first because it is a pretty trivial change and no real time lost if it isn't accepted. Thanks

Copy link

stackblitz bot commented Oct 12, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@userquin
Copy link
Member

userquin commented Oct 20, 2024

This PR should be breaking, we need to redirect types/svelte.d.ts to types/svelte5.d.ts adding types/svelte5.d.ts to the package.json exports entry, just below types/svelte4.d.ts.

We also need:

  • add entry in README file about using Svelte 4 (copy/paste entry for "Using Svelte 3" and update with v4).
  • update examples/vite-svelte and examples/sveltekit to use v4 in the corresponding src/vite-env.d.ts and src/app.d.ts respectively or try to bump both examples to use latest Svelte 5 and SvelteKit.

We don't use that formatting, enable eslint or try running pnpm lint:fix:

svelte5.d.ts
declare module 'virtual:icons/*' {
  import { Component } from 'svelte'
  import type { SvelteHTMLElements } from 'svelte/elements'

  export default Component<SvelteHTMLElements['svg']>
}

declare module '~icons/*' {
  import { Component } from 'svelte'
  import type { SvelteHTMLElements } from 'svelte/elements'

  export default Component<SvelteHTMLElements['svg']>
}

@mattdavis90
Copy link
Author

At the point of filing the PR Svelte5 was still in RC so I didn't update the default but happy to make that change now that it is released. Ref linting, sorry thought I'd run eslint but apparently not.

@userquin
Copy link
Member

userquin commented Oct 20, 2024

Yeah, I just merged Svelte 5 support a few minutes ago in main branch (I mean the PR about adding svelte 5 in supported versions)

@mattdavis90
Copy link
Author

I force pushed so that my comment is in line with conventional commit messages. The examples look to be working correctly so remain unchanged

@userquin
Copy link
Member

Add svelte5.d.ts to packages exports below this https://github.com/unplugin/unplugin-icons/blob/main/package.json#L112-L114

@userquin
Copy link
Member

userquin commented Oct 20, 2024

The example will require to update Svelte and some other dependencies (SvelteKit for example).

We also need to update examples/vite-svelte dependencies.

@userquin userquin changed the title Add Svelte5 specific types feat!: add Svelte5 specific types Oct 20, 2024
@userquin
Copy link
Member

examples/sveltekit:

                "@sveltejs/kit": "^2.7.2",
		"@sveltejs/vite-plugin-svelte": "^4.0.0",
		"svelte": "^5.0.3",
		"svelte-check": "^4.0.5",

Copy link

socket-security bot commented Oct 20, 2024

@mattdavis90
Copy link
Author

I think I've got all the dependencies and updated to svelte5 runes in the example. Having a little trouble testing because pnpm isn't playing nice with the example workspaces

@userquin
Copy link
Member

Yeah, in fact it is using Svelte 4, if you want to check it, just add a console.log below the version import with it and runnes flag:

console.log('Svelte version:', VERSION, 'svelteRunes:', svelteRunes)

image

@userquin
Copy link
Member

userquin commented Oct 20, 2024

pnpm lock file is missing in the PR and should be changed, doing weird things...

Maybe we should add empty pnpm-workspace.yaml to both svelte examples and exclude them from the workspaces (pnpm) like we did with nuxt3.

@mattdavis90
Copy link
Author

I got the workspace examples working, as you noted it was the missing pnpm-lock - no idea what my pnpm install was doing!

@mattdavis90
Copy link
Author

Both examples seem to be working now. I had a bit of a mess with my pnpm install. I cleared out all of the node_modules and they're both working now.

@userquin
Copy link
Member

I got the workspace examples working, as you noted it was the missing pnpm-lock - no idea what my pnpm install was doing!

pnpm doing the same on my local (lock file unchanged)

@userquin
Copy link
Member

/cc @dominikg @benmccann can you review this 🙏 when you have some free time?

@userquin
Copy link
Member

userquin commented Oct 20, 2024

@mattdavis90 can you check svelte compiler? maybe we can also remove the ts-expect-error (eslint should be complaining about that annotation)

@mattdavis90
Copy link
Author

I'm still getting a TS error if I remove that annotation. I haven't added svelte to dependencies, peer, or dev - it is in the lock file though for the examples

@userquin
Copy link
Member

userquin commented Oct 21, 2024

We need to review svelte 5 dts, seems to be not working, there is no auto completion in vscode, svg attrs should be there.

@mattdavis90
Copy link
Author

I can't find a better type for it. Component is the new type for components and expects props as the first generic which SvelteHTMLElements['svg'] provides. Having said that you're totally correct and I also don't see any auto complete in neovim. I hadn't actually checked that - I mostly just wanted to remove the errors around Component vs SvelteComponent. Having tested for the last hour or so I can't get completion working

@userquin
Copy link
Member

I can't find a better type for it. Component is the new type for components and expects props as the first generic which SvelteHTMLElements['svg'] provides. Having said that you're totally correct and I also don't see any auto complete in neovim. I hadn't actually checked that - I mostly just wanted to remove the errors around Component vs SvelteComponent. Having tested for the last hour or so I can't get completion working

imagen

@mattdavis90
Copy link
Author

Its now working for you? Did you change something? I still get no completion in nvim

@userquin
Copy link
Member

declare module 'virtual:icons/*' {
  import { Component } from 'svelte'
  import type { SvelteHTMLElements } from 'svelte/elements'

  type Type<K> = {
    [P in keyof K]: K[P]
  }

  const component: Component<Type<SvelteHTMLElements['svg']>>

  export default component
}

declare module '~icons/*' {
  import { Component } from 'svelte'
  import type { SvelteHTMLElements } from 'svelte/elements'

  type Type<K> = {
    [P in keyof K]: K[P]
  }

  const component: Component<Type<SvelteHTMLElements['svg']>>

  export default component
}

@userquin
Copy link
Member

Remove the type, just split the component + default export:

declare module 'virtual:icons/*' {
  import { Component } from 'svelte'
  import type { SvelteHTMLElements } from 'svelte/elements'

  const component: Component<SvelteHTMLElements['svg']>

  export default component
}

declare module '~icons/*' {
  import { Component } from 'svelte'
  import type { SvelteHTMLElements } from 'svelte/elements'

  const component: Component<SvelteHTMLElements['svg']>

  export default component
}

imagen

@mattdavis90
Copy link
Author

Cool - you totally beat me to it - this is what I was experimenting with. I hadn't got the keyof bit quite working yet

declare module "virtual:icons/*" {
  import type { Component } from "svelte";
  import type { SVGAttributes } from "svelte/elements";

  const c: Component<SVGAttributes<SVGSVGElement>>;
  export = c;
}

@userquin
Copy link
Member

userquin commented Oct 21, 2024

Cool - you totally beat me to it - this is what I was experimenting with. I hadn't got the keyof bit quite working yet

declare module "virtual:icons/*" {
  import type { Component } from "svelte";
  import type { SVGAttributes } from "svelte/elements";

  const c: Component<SVGAttributes<SVGSVGElement>>;
  export = c;
}

This is TS: export { c } should also work => import { c as XXX } from 'virtual:icons/...

@mattdavis90
Copy link
Author

Neither of your solutions give me completions in nvim but if they're working in VSCode them I'm happy to put it down to a client issue

@userquin
Copy link
Member

Change svelte5.d.ts: next time we should check any d.ts at root ;)

@userquin
Copy link
Member

Neither of your solutions give me completions in nvim but if they're working in VSCode them I'm happy to put it down to a client issue

Did you restarted ts service?

@mattdavis90
Copy link
Author

Neither of your solutions give me completions in nvim but if they're working in VSCode them I'm happy to put it down to a client issue

Did you restarted ts service?

Yeh, it works if I do Component<{abc: number}> but no suggestions for Component<SVGAttributes<SVGSVGElement>> or Component<SvelteHTMLElements['svg']> - I'll check the logs and restart nvim

@mattdavis90
Copy link
Author

It is working in a new project - looks like it is just broken in my local where I was overriding the types in app.d.ts

@userquin
Copy link
Member

WebStorm/IntelliJ will require some update to support kit 5 😅

imagen

Copy link
Member

@userquin userquin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx ❤️

@mattdavis90
Copy link
Author

Thanks for the help :)

@mattdavis90
Copy link
Author

@mattdavis90 can you check svelte compiler? maybe we can also remove the ts-expect-error (eslint should be complaining about that annotation)

I just noticed in #382 that svelte is added as a peer dependency so maybe you're right and we can drop the ts-expect-error now?

@userquin
Copy link
Member

@mattdavis90 can you check svelte compiler? maybe we can also remove the ts-expect-error (eslint should be complaining about that annotation)

I just noticed in #382 that svelte is added as a peer dependency so maybe you're right and we can drop the ts-expect-error now?

That's why I was asking you ;)

We can even dispatch click events:

imagen

/cc @dominikg @benmccann

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.

3 participants