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

Show exported functions in auto-complete menu #307

Closed
geoffrich opened this issue Jul 14, 2020 · 9 comments
Closed

Show exported functions in auto-complete menu #307

geoffrich opened this issue Jul 14, 2020 · 9 comments
Labels
feature request New feature or request Fixed Fixed in master branch. Pending production release.

Comments

@geoffrich
Copy link
Member

Is your feature request related to a problem? Please describe.
When binding to a component instance, I would like to have any exported functions available for auto-completion.

Describe the solution you'd like
Given the following components:

<!-- App.svelte -->
<script>
  import ClickTracker from "./ClickTracker.svelte";

  let componentRef;
</script>

<main>
  <ClickTracker bind:this={componentRef} />
  <button on:click={componentRef.track}>Click me</button>
</main>

<!-- ClickTracker.svelte -->
<script>
  export function track() {
    clicks++;
  }

  let clicks = 0;
</script>

<p>Clicks: {clicks}</p>

When operating on componentRef, I would like to see the track function show in the auto-complete menu.
image

Additional context
When using TypeScript with svelte-preprocess, referencing the track function shows the warning "Property 'track' does not exist on type 'ClickTracker__SvelteComponent_'.ts(2339)" under the componentRef.track reference. I think this could be related.

<!-- App.svelte -->
<script lang="ts">
  import ClickTracker from "./ClickTracker.svelte";

  let componentRef: ClickTracker;
</script>

<main>
  <ClickTracker bind:this={componentRef} />
  <button on:click={componentRef.track}>Click me</button>
</main>
@jasonlyu123 jasonlyu123 added the feature request New feature or request label Jul 15, 2020
@opensas
Copy link
Contributor

opensas commented Jul 17, 2020

I had a similar issue and found the following workaround, although is too much code for my taste

<!-- ClickTracker.svelte -->
<script context='module' lang='ts'>
  export type ClickTrackerType =  SvelteComponent & { track(): void }
</script>
[...]

Then, from App.svelte:

<!-- App.svelte -->
<script lang='ts'>
  import ClickTracker from "./ClickTracker.svelte";
  import type { ClickTrackerType }  from "./ClickTracker.svelte";

  let componentRef: ClickTrackerType;
</script>

On the other hand, inspecting from vscode I found a

@geoffrich
Copy link
Member Author

Hm, yeah, that's a good workaround. Though it's definitely more code than is ideal. I also don't like how any changes to the track() function would require manual updates to the exported type as well.

@opensas
Copy link
Contributor

opensas commented Jul 20, 2020

@geoffrich of course it's just a workaround until this issue is resolved, supporting typescript means that svelte can correctly generate the typings for my own Svelte component.

For me, the ideal syntax would be something like this:

let componentRef: ClickTracker

and that's it...

@opensas
Copy link
Contributor

opensas commented Jul 27, 2020

Has this been solved? Trying the ClickTracker example with typescript still gives me the following error:

Property 'track' does not exist on type 'ClickTracker__SvelteComponent_'.ts(2339)

I'm working recently created from sveltejs/template, with the setupeNode.js script.

These are my devDependencies

  "devDependencies": {
    "@rollup/plugin-commonjs": "^12.0.0",
    "@rollup/plugin-node-resolve": "^8.0.0",
    "rollup": "^2.3.4",
    "rollup-plugin-livereload": "^1.0.0",
    "rollup-plugin-svelte": "^5.0.3",
    "rollup-plugin-terser": "^5.1.2",
    "svelte": "^3.0.0",
    "svelte-check": "^0.1.0",
    "svelte-preprocess": "^4.0.0",
    "@rollup/plugin-typescript": "^4.0.0",
    "typescript": "^3.9.3",
    "tslib": "^2.0.0",
    "@tsconfig/svelte": "^1.0.0"
  },

@dummdidumm
Copy link
Member

The new VSCode Plugin version was just released, maybe your IDE did not update yet. Newest version is 101.4.0 .

@opensas
Copy link
Contributor

opensas commented Jul 27, 2020

Excellent!!!

@vultix
Copy link

vultix commented Apr 21, 2021

@opensas @dummdidumm I'm still seeing this issue.

Svelte plugin: 104.9.1

Steps to reproduce:

  • Setup a new typescript svelte app:
npx degit sveltejs/template svelte-app
cd svelte-app
node ./scripts/setupTypeScript.js
  • Create a new file src/ClickTracker.svelte with the following:
<script>
    export function track() {
        clicks++;
    }

    let clicks = 0;
</script>

<p>Clicks: {clicks}</p>
  • Modify src/App.svelte to the following:
<script lang="ts">
	import ClickTracker from "./ClickTracker.svelte";
  
	let componentRef: ClickTracker;
</script>

<main>
	<ClickTracker bind:this={componentRef} />
	<button on:click={componentRef.track}>Click me</button>
</main>
  • Note the error in src/App.svelte: Property 'track' does not exist on type 'ClickTracker__SvelteComponent_'

@dummdidumm
Copy link
Member

I'm not able to reproduce this, works for me.

@dummdidumm
Copy link
Member

This repository isn't about the jetbrains svelte plugin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request Fixed Fixed in master branch. Pending production release.
Projects
None yet
Development

No branches or pull requests

5 participants