在 svelte 文件中导出打字稿类型 [英] export typescript type in svelte file

查看:27
本文介绍了在 svelte 文件中导出打字稿类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想导出我在其中一个文件中定义的类型并将其导入到另一个文件中.

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};

当我添加 export 时,linter 显示以下错误:

linter show me bellow error when I add export:

修饰符不能出现在这里.

我可以通过创建一个新的 ts 文件并从中导入类型来使其工作.我只想知道是否有办法在 svelte 文件中定义类型.

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.

更新:

我使用 sapper 模板,它可以正常运行,但 TS 功能不起作用,并在从 svelte 文件导入类型和导出类型时显示 vscode 中的错误.

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.

推荐答案

您需要从模块脚本中导出类型,而不是普通脚本.您还需要在普通脚本或模块脚本上添加 lang=ts" 属性.这将起作用:

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>

通常,每当您想从不是组件本身的另一个 Svelte 文件中导入某些内容时,您都需要在模块脚本中声明该导出.

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.

这篇关于在 svelte 文件中导出打字稿类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆