[英]Type “only refers to a type, but is being used as a value here” when exporting
I have a file called types.ts
which has all my types in. I want to export some of these, like so:我有一个名为types.ts
的文件,其中包含我所有的类型。我想导出其中的一些,如下所示:
export type Example {
something: string;
}
I have another file called index.ts
, which is the entry point for my code.我有另一个名为index.ts
的文件,它是我的代码的入口点。 I want to export the Example type.我想导出 Example 类型。 When I try the following:当我尝试以下操作时:
import { Example } from "./types";
export default {
Example
}
I get the following error:我收到以下错误:
'Example' only refers to a type, but is being used as a value here.ts(2693) “示例”仅指一种类型,但在此处用作值。ts(2693)
I'm not sure how to export a type from another file correctly.我不确定如何正确地从另一个文件中导出类型。 I have also tried the following:我还尝试了以下方法:
export * from "./types";
export { Example } from "./types";
But this doesn't work as it's not part of the export, due to my export default
which contains other stuff, however this might be an entirely different issue if this is the correct way to do it.但这不起作用,因为它不是导出的一部分,因为我的export default
包含其他内容,但是如果这是正确的方法,这可能是一个完全不同的问题。
What's the correct / best way to achieve this?实现这一目标的正确/最佳方法是什么?
When you do当你这样做
export default {
Example
}
...you're exporting an object literal with an Example
property (written with shorthand notation) as the default export of a module. ...您正在导出一个 object 文字和一个Example
属性(用简写符号编写)作为模块的默认导出。 That means it's expecting Example
to be a variable whose value will be copied to the Example
property of the object being exported.这意味着它期望Example
是一个变量,其值将被复制到正在导出的 object 的Example
属性中。 But in your case, Example
is a type, not a variable.但是在您的情况下, Example
是一种类型,而不是变量。
If you want to export Example
as the default export of a module, you'd do it like this:如果要将Example
导出为模块的默认导出,您可以这样做:
export default Example;
If you want to export Example
as a named type export, you don't use default
:如果要将Example
导出为命名类型导出,则不要使用default
:
export { Example };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.