I'm trying to create a svelte component (using TypeScript), create a type, export it and import it into another file.
Options.svelte
=> svelte component that also exports typeindex.svelte
=> import component and use typeI have a component, for example:
Options.svelte
<script lang="ts" context="module">
export type Option = {
label: string
};
</script>
<script lang="ts">
export let options: Option[]
</script>
{#each options as option}
<div>{option.label}</div>
{/each}
And I use that component in another file, for example:
index.svelte
<script lang="ts">
import Options from './Options.svelte'
import type Option from './Options.svelte'
let options: Option[] = [{ label: 'one' }, { label: 'two' }]
</script>
<Options bind:options />
This continues to give me the following error when running svelte-check --ignore src/node_modules/@sapper
:
Error: Type '{ label: string; }' is missing the following properties from type 'SvelteComponentDev': $set, $on, $destroy, $capture_state, and 2 more. (ts)
Error: JSX element class does not support attributes because it does not have a '$$prop_def' property. (ts)
Error: Type definitions are missing for this Svelte Component. It needs a class definition with at least the property '$$prop_def' which should contain a map of input property definitions.
Example:
class ComponentName { $$prop_def: { propertyName: string; } }
'SwitchMulti' cannot be used as a JSX component.
Its instance type 'SvelteComponentDev' is not a valid JSX element.
Property '$$prop_def' is missing in type 'SvelteComponentDev' but required in type 'ElementClass'. (ts)
How might I export/import the TypeScript type/interface from the svelte component to the index.svelte
file so that I may use it?
I followed what is written in this answer, but cannot figure out how to import it without constant errors. Is this an issue with svelte-check
?
I can confirm that this issue is specific to svelte-check
(current version 1.1.17)
.
I can run sapper dev
and there are no errors.
As mentioned by dummdidumm in their answer, the first error is result of a typo, I should instead have the following import (I was importing it as default - which is the component itself, not the type):
import type { Option } from './Options.svelte'
The other errors still persist whenever I pass an attribute to a custom component built with TypeScript.
Once you have TypeScript configured, you can start using it from a Svelte component by just adding a <script lang='ts'> at the beginning of the script section. To use it from regular JavaScript files, just change the file extension from . js to . ts .
TypeScript supports export = to model the traditional CommonJS and AMD workflow. The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum.
You probably don't need TypeScript directly to profit from a strong type-checking experience inside your Svelte and SvelteKit applications. VS Code and the Svelte extension can also offer help if you annotate your components with JSDoc comments.
The error is misleading. You import the type as a default import. The default import is the component. You should write import type { Option } from './Options.svelte'
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With