DEV Community

oliviarizona
oliviarizona

Posted on

TS1195: 'export _' does not re-export a default

TS1195: 'export _' does not re-export a default

TypeScript is a powerful superset of JavaScript that adds static types. This means you can define the types of variables, function parameters, and return values, which helps catch errors at compile time instead of runtime. Types are essentially a way to describe the shape and characteristics of data, enabling developers to write more reliable code.

If you're looking to deepen your understanding of TypeScript or to learn how to code using AI tools like gpteach, I recommend subscribing or following my blog for more insights!

What is a Superset Language?

A superset language is one that builds upon another language, adding new features and capabilities while maintaining compatibility with the original language. In the case of TypeScript being a superset of JavaScript, it means all valid JavaScript code is also valid TypeScript code. TypeScript introduces additional features, such as type annotations, interfaces, and enums, which enhance the development experience.

Understanding TS1195: 'export _' does not re-export a default.

The error TS1195: 'export _' does not re-export a default typically occurs in TypeScript when you're trying to export something that doesn't behave as a default export from a module. Understanding this requires a brief look at how exports work in TypeScript.

What are Exports in TypeScript?

In TypeScript and JavaScript, you can export objects, functions, classes, or variables from a module so they can be imported and used in other modules. There are two main types of exports:

  1. Default Exports: You can only have one default export per file. They are imported without curly braces.
   export default function greet() {
       console.log("Hello, world!");
   }
Enter fullscreen mode Exit fullscreen mode
  1. Named Exports: You can have multiple named exports in a module. They must be imported with curly braces.
   export function farewell() {
       console.log("Goodbye, world!");
   }
Enter fullscreen mode Exit fullscreen mode

The TS1195 Error Explained

When you see TS1195: 'export _' does not re-export a default, it indicates that you are trying to re-export a default export incorrectly. This often leads to confusion when manipulating exports across different files.

Example That Causes the Error

Let's look at an example that could trigger this error:

File: moduleA.ts

export default function myFunction() {
    return "Hello from myFunction!";
}
Enter fullscreen mode Exit fullscreen mode

File: moduleB.ts

export { myFunction } from './moduleA';
Enter fullscreen mode Exit fullscreen mode

Here, moduleB.ts tries to re-export myFunction, which is a default export from moduleA.ts. This will lead to the TS1195 error.

How to Fix TS1195

To fix the error, you simply need to re-export the default export correctly. You can do this by assigning a name when importing it.

Corrected moduleB.ts

import myFunction from './moduleA'; // Import the default export

export { myFunction }; // Now we can re-export it
Enter fullscreen mode Exit fullscreen mode

Important to Know!

  • Ensure you understand the difference between default and named exports to prevent this type of error.
  • When you've defined a default export in one module, always import it as a whole entity and then re-export it if needed.

Important to Know!

When you see the error TS1195: 'export _' does not re-export a default, check your import statements!

FAQ about TS1195 and TypeScript

Q1: What does it mean to re-export?
A: Re-exporting allows you to create a central point for exports from multiple modules, making it easier to manage imports.

Q2: Can I have multiple default exports in a file?
A: No, each module can only have one default export. If you need multiple, you should use named exports instead.

Conclusion

In conclusion, the TypeScript error TS1195: 'export _' does not re-export a default is a common mistake rooted in misunderstanding TypeScript's export system. By following proper import/export syntax and being mindful of how defaults work, you can avoid this error in your TypeScript projects.

If you wish to learn more about TypeScript concepts, or if you're looking to improve your coding skills with tools like gpteach, make sure to subscribe or follow my blog for continual learning and insights!

Top comments (0)