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

support type annotations in javascript as jsdoc comments #1452

Open
DetachHead opened this issue Apr 20, 2022 · 1 comment
Open

support type annotations in javascript as jsdoc comments #1452

DetachHead opened this issue Apr 20, 2022 · 1 comment
Labels
feature request New feature or request

Comments

@DetachHead
Copy link
Contributor

DetachHead commented Apr 20, 2022

Describe the problem

while attempting to do a type cast while working around sveltejs/svelte#4701, i noticed that jsdoc comment types aren't supported

Describe the proposed solution

support type casts in jsdoc comments:

<Dropdown items="{/** @type {Foo} */ (items)}" />

Alternatives considered

Importance

would make my life easier

@jasonlyu123 jasonlyu123 transferred this issue from sveltejs/svelte Apr 20, 2022
@jasonlyu123
Copy link
Member

In non typescript components, i.e. no lang="ts" or something similar. This works in some areas. But it mostly doesn't work in the new transformation.

In typescript component. I don't think this will ever be supported. In the current implementation. The whole svelte component is transformed into a single .tsx file or a .ts file in the new transformation. This means there's no way it can partly be treated as js.
In this case, I would suggest using a function in the script to type assert instead.

<script>
function castAsFoo(_items) {
  return _items as Foo
}
</script>

<Dropdown items={castAsFoo(items)} />

Or use control flow to narrow the type

<script>
function isFoo(_items): items is Foo {
  return 'foo' in items
}
</script>

{#if isFoo(items)}
<Dropdown items={castAsFoo(items)} />
{/if}
{#if  'foo' in items}
<Dropdown items={castAsFoo(items)} />
{/if}

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
Projects
None yet
Development

No branches or pull requests

3 participants