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

Type annotations for module="context" scripts exports #56

Closed
flekschas opened this issue Dec 19, 2021 · 3 comments · Fixed by #70
Closed

Type annotations for module="context" scripts exports #56

flekschas opened this issue Dec 19, 2021 · 3 comments · Fixed by #70
Labels
bug Something isn't working

Comments

@flekschas
Copy link

flekschas commented Dec 19, 2021

First of all, huge thanks for this library! 🎉 It's a joy to work with.

I'm running into an issue with global exports defined in <script context="module" /> blocks. The generated type annotation does not properly reflect the static export.

Say for example I have the following single component:

<!-- welcome.svelte -->
<script context="module">
  /**
   * Log something
   * @type {(message: string) => void}
   */
  export function log(message) {
    console.log(message);
  }
</script>

<script>
  /**
   * Welcome message
   * @type string
   */
  export let message = 'there';
</script>

<h1>Hi {message}</h1>

To generate type definitions with Sveld I'd add the following index.js file:

export { default, default as Welcome, log } from './welcome.svelte';

Say I publish this package as my-cool-welcome-svelte-component, then in another typescript-powered Svelte app I can import the component and log() as expected:

<script lang="ts">
  import Welcome, { log } from 'my-cool-welcome-svelte-component';
  log('something interesting');
</script>

<Welcome message="cool user" />

However, TS will error and say that log is not exported by my-cool-welcome-svelte-component. I am not a TypeScript expert, so please forgive if my thinking is incorrect, but the issue appears to be that log will be typed as a static class method rather than a normal export.

Currently the types/welcome.svelte.d.ts looks as follows:

/// <reference types="svelte" />
import { SvelteComponentTyped } from "svelte";

export interface WelcomeProps, {
  /**
   * Welcome message
   * @default 'there'
   */
  message: string;
}

export default class Welcome extends SvelteComponentTyped<
  WelcomeProps,
  { },
  { default: {} }
> {
  /**
   * Log something
   */
  log: (message: string) => void;
}

Is there an issue with my setup or is this a limitation of Sveld? If it's a limitation, do you guys by chance have an idea how one could still make log() appear as a normal export type log = (message: string) => void;?

@metonym
Copy link
Collaborator

metonym commented Feb 13, 2022

Thank you for pointing this out. This is a bug; the content in a context module should not be recognized as accessors.

@metonym metonym added bug Something isn't working and removed enhancement New feature or request labels Feb 13, 2022
@metonym
Copy link
Collaborator

metonym commented Feb 13, 2022

@flekschas Fixed in v0.13.3

@flekschas
Copy link
Author

Thanks a lot for fixing the issue @metonym! I'll give the new version a try right

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants