language-tools
language-tools copied to clipboard
support type annotations in javascript as jsdoc comments
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
- wait for sveltejs/svelte#4701 so i can use an actual type cast ie.
<Dropdown items="{items as Foo}" /> - suppress the type error using this workaround https://github.com/sveltejs/language-tools/issues/1026#issuecomment-1002839154
Importance
would make my life easier
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}