Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

export typescript type in svelte file

I want to export the type that I defined in one of my files and import it inside another file.

export type myNewType = {name: string};

linter show me bellow error when I add export:

Modifiers cannot appear here.

I can make it work by creating a new ts file and import the type from it. I just want to know if there is a way to define type inside svelte file or not.

Update:

I use the sapper template and it will run without error but TS functionality not work and show me errors in vscode when importing type and export type from svelte file.

like image 903
Alireza Avatar asked Sep 25 '20 12:09

Alireza


People also ask

Can you export a type in TypeScript?

Use a named export to export a type in TypeScript, e.g. export type Person = {} . The exported type can be imported by using a named import as import {Person} from './another-file' . You can have as many named exports as necessary in a single file.

How do I import TypeScript into Svelte?

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 .

How do I export a TypeScript function?

Use named exports to export a function in TypeScript, e.g. export function sum() {} . The exported function can be imported by using a named import as import {sum} from './another-file' . You can use as many named exports as necessary in a single file.

How do I export a component in Svelte?

Exporting a Svelte component </h1> ); export default GreetMessage; Once you've created your class or functional React component, you must use the export keyword or you can use export default to export that React component. In Svelte, by default it's exported as soon you create your Svelte file!


2 Answers

You need export the type from a module-script, not the normal script. You also need to add the lang="ts" attribute on either the normal script or the module-script. This will work:

<script context="module" lang="ts">
  export type myNewType = {name: string};
</script>

<script>
  export let aProp: string;
</script>

<p>some html</p>

In general, whenever you want to import something from another Svelte file which is not the component itself, you need to declare that export inside the module-script.

like image 173
dummdidumm Avatar answered Sep 22 '22 14:09

dummdidumm


Just in case it helps anyone, I was having an issue importing my own type which I had declared in a .ts file, into another library file:

My type was declared as follows:

export type UserAuth = {
    name: string,
    email: string,
    token: ''
};

An in my /lib/auth.ts file, I was attempting to import it as follows:

import { UserAuth } from "../types/user.auth";

Which produced an error that the type has not been exported.

The following fixed the issue for me:

import type { UserAuth } from "../types/user.auth";
like image 34
Musaffar Patel Avatar answered Sep 23 '22 14:09

Musaffar Patel