最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Type "only refers to a type, but is being used as a value here" when exporting - Stack Overflow

programmeradmin0浏览0评论

I have a file called types.ts which has all my types in. I want to export some of these, like so:

export type Example {
  something: string;
}

I have another file called index.ts, which is the entry point for my code. I want to export the Example type. 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)

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.

What's the correct / best way to achieve this?

I have a file called types.ts which has all my types in. I want to export some of these, like so:

export type Example {
  something: string;
}

I have another file called index.ts, which is the entry point for my code. I want to export the Example type. 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)

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.

What's the correct / best way to achieve this?

Share Improve this question asked Jul 11, 2020 at 22:12 user8826104user8826104
Add a ment  | 

1 Answer 1

Reset to default 5

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. That means it's expecting Example to be a variable whose value will be copied to the Example property of the object being exported. But in your case, Example is a type, not a variable.

If you want to export Example as the default export of a module, you'd do it like this:

export default Example;

If you want to export Example as a named type export, you don't use default:

export { Example };

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论