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 Action as a type for use:action #7347

Closed
wants to merge 2 commits into from

Conversation

ivanhofer
Copy link
Contributor

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with [feat], [fix], [chore], or [docs].
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with npm test and lint the project with npm run lint

Currently there is no type definition for a Svelte action defined. This PR adds a new Action type that get's exposed by the svelte package.

This can be used to type actions that are declared outside of a Svelte component.

action.ts

import type { Action } from 'svelte'

export const myAction: Action<HTMLTextAreaElement, boolean> (node, param) => {
   // `node` and `param` are typed 
   return {
      update: (param) => void 0
      destroy: () => void 0
   }
}

export const myOtherAction: Action<HTMLTextAreaElement> (node) => {
   return {
      destroy: () => void 0
   }
}

The benefit of this type is that you will get an error if you define something wrong. e.g. you misspelled 'update' as 'updates'.

The Svelte VS Code extension already throws an error if you try to apply the action use:myAction, but only if you have opened that file that uses the action. You won't see an error if you only open the action.ts file in your IDE.

@dummdidumm
Copy link
Member

See #7121, this already exists to some extent. The "void extends" clause is interesting for differentiation though, maybe we can incorporate that.

@ivanhofer
Copy link
Contributor Author

uups, forgot to rebase so I haven't seen this type in my forked repo😅

@tanhauhau
Copy link
Member

Close in favor of #7442

i've incorporated your idea for "void extends" into #7121, and attributed you as co-author. thanks!

@tanhauhau tanhauhau closed this Apr 11, 2022
@ivanhofer
Copy link
Contributor Author

@tanhauhau thanks, I completely forgot about this

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.

None yet

3 participants