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

How do I type custom events for a package? #1243

Open
ghostdevv opened this issue Nov 16, 2021 · 4 comments
Open

How do I type custom events for a package? #1243

ghostdevv opened this issue Nov 16, 2021 · 4 comments
Labels
documentation Improvements or additions to documentation feature request New feature or request question Further information is requested

Comments

@ghostdevv
Copy link
Member

I was wondering what would be the best way to type custom events for a package, puru was recently doing this and now I am too - while you can type the events like this (https://discord.com/channels/457912077277855764/728292755087818924/908761587660255313) I don't know of a way to export those types out of a package and have the language server pick up on that

Any ideas?

@dummdidumm dummdidumm added feature request New feature or request question Further information is requested labels Nov 16, 2021
@dummdidumm
Copy link
Member

I guess you talking about custom events in the context of actions. If not, the solution below doesn't apply.

This came up in Discord, too, recently, here is the thread: https://discord.com/channels/457912077277855764/897113912422580274 . It didn't seem to work for that person but I didn't look into why exactly. If you could try the steps and report back if it works that would be great. The steps (roughly):

  1. create a types.d.ts (or whatever you call it) inside your package, enhance the typings like in your Discord link - make sure to keep this an ambient type, so no top level imports/exports allowed
  2. import that file in your index.d.ts which is the default types export like import './types.d.ts';

If you have your package publicly somewhere, I could take a look.

I also plan on introducing support for typing the events of an action on the action function itself. So basically you would be able to hint at typescript that by applying an action to a DOM element that element now has additional attributes/events that can be passed to it. This depends on changes to Svelte core and a pending rewrite of svelte2tsx, so this will take a little while.

@ghostdevv
Copy link
Member Author

ghostdevv commented Nov 19, 2021

I wrote this package as a test, it has a testAction which emits a custom event called test, I don't get intellisense suggesting that test is a event (like I do with other events) but the actual typings appear to be working which is awesome:

Screenshot from 2021-11-19 09-39-10

@dummdidumm
Copy link
Member

Yes, autocomplete intellisense is missing because that needs some more work on our side to make it show up (not sure how we can get there without showing all other redundant completions).
Good to hear that this works, I guess we need to document the way how to do this once the rewrite happened and we are in a more stable state to officially recommend the best way to do this.

@dummdidumm dummdidumm added the documentation Improvements or additions to documentation label Nov 19, 2021
@ghostdevv
Copy link
Member Author

Yea would be awesome to be able to do this but glad to know that this does work just missing intellisense - how would you recommend doing this with sveltekit package for example?

dummdidumm added a commit that referenced this issue Aug 26, 2022
#1243
#431

This assumes we also enhance the typing of the action type in Svelte core so they match
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation feature request New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants