user8826104
user8826104

Reputation:

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:

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?

Upvotes: 5

Views: 13961

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075597

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

Upvotes: 5

Related Questions