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

[TypeScript] Action Type #6538

Closed
NickClark opened this issue Jul 16, 2021 · 6 comments · Fixed by #7121
Closed

[TypeScript] Action Type #6538

NickClark opened this issue Jul 16, 2021 · 6 comments · Fixed by #7121
Labels
runtime Changes relating to runtime APIs

Comments

@NickClark
Copy link

NickClark commented Jul 16, 2021

Describe the bug

An action type should be available to developers. One is available if you stay within a *.svelte file, but isn't present outside of it.

Reproduction

Define an action in your svelte file:

function onResize(
  node: HTMLDivElement,
  callback: (entry: DOMRectReadOnly) => void,
): SvelteActionReturnType {
  // ...
}

and the types work.

Move the action outside to its own module, and you no longer have any types you can import to ensure action type.

Maybe something like this would work:

type SvelteActionReturnType<P> = {
  update?: (newParams: P) => void;
  destroy?: () => void;
} | void;

type SvelteActionType<P> = (
  node: Element,
  params: P,
) => SvelteActionReturnType<P>

Logs

No response

System Info

System:
    OS: Linux 5.4 Ubuntu 20.04.2 LTS (Focal Fossa)
    CPU: (24) x64 AMD Ryzen 9 5900X 12-Core Processor
    Memory: 14.89 GB / 24.99 GB
    Container: Yes
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.2.0 - ~/.asdf/installs/nodejs/16.2.0/bin/node
    npm: 7.13.0 - ~/.asdf/installs/nodejs/16.2.0/bin/npm
  npmPackages:
    svelte: ^3.38.2 => 3.38.2
    vite: ^2.3.7 => 2.3.7

Severity

annoyance

@tanhauhau
Copy link
Member

@dummdidumm do we already have svelte action type definition defined in language tools?

@benbender
Copy link
Contributor

@tanhauhau @dummdidumm Doesn't seem so and would be a very nice addition! I was searching for one just yet and couldn't find it...

PS: Many thanks for your tremendous work on the tooling. Very much appreciated!

@jasonlyu123
Copy link
Member

There's only the action return type in language tools, because svelte2tsx now just transforms use:action to an action function call. There's one before and it's similar to the one OP proposed. Just the element is also a generic parameter.

@dummdidumm dummdidumm added the runtime Changes relating to runtime APIs label Aug 16, 2021
@dummdidumm
Copy link
Member

dummdidumm commented Aug 26, 2021

Such a type makes sense to add. Note to self: We should filter out the global svelte2tsx completions to avoid such confusion in the future.
Question is what's the best name and where to put this. @Conduitry what do you think about a new import path, like svelte/action or svelte/actions ? Names for the types could be Action and ... I don't know a good name for the return type 😄

@ghostdevv
Copy link
Member

Is there any progress on typing for an action?

@Conduitry
Copy link
Member

This should be fixed in 3.46.5. (Probably should have been 3.47.0, whoops.)

nevilm-lt pushed a commit to nevilm-lt/svelte that referenced this issue Apr 21, 2022
nevilm-lt pushed a commit to nevilm-lt/svelte that referenced this issue Apr 21, 2022
nevilm-lt pushed a commit to nevilm-lt/svelte that referenced this issue Apr 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
runtime Changes relating to runtime APIs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants