简体   繁体   English

导出时类型“仅指一种类型,但在此处用作值”

[英]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.

相关问题 'MapEditServiceConfig'仅引用类型,但在此处用作值 - 'MapEditServiceConfig' only refers to a type, but is being used as a value here “承诺”仅指类型,但在此处用作值 - 'Promise' only refers to a type, but is being used as a value here “'输出'仅指一种类型,但在此处使用了一个值”错误 - "'Output' only refers to a type, but is being used a value here" error “post”仅指一种类型,但在此处用作值 - 'post' only refers to a type, but is being used as a value here 元素指的是一个值,但在这里被用作类型 - element refers to a value, but is being used as a type here 'ItemIsLoading'on指的是一种类型,但此处被用作值 - 'ItemIsLoading' on refers to a type, but is being used as a value here 指的是一个值,但在这里被用作一个类型 - refers to a value, but is being used as a type here IFormContext 只引用一个类型,但在这里用作一个值 - IFormContext only refers to a type, but is used as a value here 打字稿错误:'RRule' 仅引用类型,但在此处用作命名空间 - typescript error: 'RRule' only refers to a type, but is being used as a namespace here ionic 'DocumentScanner' 指的是一个值,但在这里被用作一个类型 - ionic 'DocumentScanner' refers to a value, but is being used as a type here
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM