30

I have created multiple interfaces and want to ship them all from a common index.ts file as shown below:

--pages
------index.interface.ts
--index.ts

Now In my index.ts I am exporting something like:

export { timeSlots } from './pages/index.interface';

whereas my index.interface.ts look like this:

export interface timeSlots {
  shivam: string;
  daniel: string;
  jonathan: string;
}

Now when I try to do so, it says me:

Re-exporting a type when the '--isolatedModules' flag is provided requires using 'export type'.ts(1205)

Not sure why this error shows up, can someone help?

2 Answers 2

62

You just need to use this syntax when re-exporting types:

export type { timeSlots } from './pages/index.interface';
//     ^^^^
// Use the "type" keyword

Or, if using a version of TypeScript >= 4.5, you can use the type modifier before each exported type identifier instead:

export { type timeSlots } from './pages/index.interface';
//       ^^^^
// Use the "type" keyword

The second approach allows you to mix type and value identifiers in a single export statement:

export { greet, type GreetOptions } from './greeting-module';

Where greeting-module.ts might look like this:

export type GreetOptions = {
  name: string;
};

export function greet(options: GreetOptions): void {
  console.log(`Hello ${options.name}!`);
}
4
  • 1
    How would I do this in an AWS Code build environment? For example, I am getting this error from a library, and upon installation, it offers this typescript error.
    – Luke
    Jan 9 at 18:37
  • ^ @Luke You can ask a new question if it's still relevant (and feel free to link it here in a comment if desired).
    – jsejcksn
    Jul 19 at 20:44
  • but why does it require us to do so? Why can't it automatically recognize it's a type?
    – Ooker
    Jul 28 at 6:15
  • ^ @Ooker Identifiers can be values, types, or both — and need to be statically-analyzed for compilation- and tree-shaking-purposes.
    – jsejcksn
    Jul 28 at 7:13
0

Here is the official documentation for type-only exports.

This feature is something most users may never have to think about; however...

...export type only provides an export that can be used for type contexts, and is also erased from TypeScript’s output.

To fix the issue, update your code to this:

export type { timeSlots } from './pages/index.interface';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.