43

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?

3 Answers 3

78

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
    Commented Jan 9, 2023 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
    Commented Jul 19, 2023 at 20:44
  • but why does it require us to do so? Why can't it automatically recognize it's a type?
    – Ooker
    Commented Jul 28, 2023 at 6:15
  • ^ @Ooker Identifiers can be values, types, or both — and need to be statically-analyzed for compilation- and tree-shaking-purposes.
    – jsejcksn
    Commented Jul 28, 2023 at 7:13
4

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';
4

you can change true to false for isolatedModules in your source project tsconfig.json.

{
  "compilerOptions": {
    ...
    "isolatedModules": false,  //true to false
    ...
  }
}

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