DEV Community

Turing
Turing

Posted on

TS1193: An export declaration cannot have modifiers

TS1193: An export declaration cannot have modifiers

TypeScript is a powerful programming language that serves as a strict syntactical superset of JavaScript, meaning that it builds upon JavaScript by adding optional static typing. Types (which are essential classifiers for data in programming) help developers avoid many runtime errors by catching mistakes during development. For instance, a variable can be explicitly defined as a string type, ensuring that only string values can be assigned to it.

If you're eager to learn more about TypeScript or want to explore how to use AI tools like gpteach to become a better coder, be sure to subscribe to my blog!

What are Types?

In TypeScript, types define the shape and behavior of data structures. They can describe primitive values like string, number, and boolean, or complex values like objects and arrays. By defining explicit types, TypeScript enables developers to understand what kind of data is expected, leading to better code readability and maintenance.

TS1193: An export declaration cannot have modifiers

The error code TS1193: An export declaration cannot have modifiers occurs when you attempt to use access modifiers (like public, private, or protected) or other TypeScript language modifiers (like readonly) with an export declaration. The goal of this restriction is to maintain a clear distinction between access control at the class or interface level and how data is made available through export declarations.

Let’s look at some examples to illustrate this:

Example that Causes TS1193 Error

Here’s a common case that triggers this error:

export private class User {
  constructor(public name: string) {}
}
Enter fullscreen mode Exit fullscreen mode

In this example, we are trying to export a class with a private modifier. However, this violates the rule because the export keyword cannot coexist with access modifiers like private.

How to Fix TS1193 Error

To fix the issue, you can either remove the access modifier from the export declaration:

class User {
  constructor(public name: string) {}
}

export { User };
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can simultaneously declare the class with the export keyword in a way that does not use modifiers:

export class User {
  constructor(public name: string) {}
}
Enter fullscreen mode Exit fullscreen mode

Important to Know!

  1. You cannot use export with any access modifier such as public, private, or protected.
  2. Ensure that when you declare a class or interface for export, it has to be exported in a way that does not conflict with these modifiers.

FAQ's

Q: What does it mean when it says an export declaration cannot have modifiers?

A: It means you cannot use access control modifiers along with the export keyword. The export keyword needs to be freely applied without modifiers that control access.

Q: How can I export types and interfaces?

A: You simply define your type or interface and use the export keyword directly in front of its declaration.

export interface User {
  name: string;
}
Enter fullscreen mode Exit fullscreen mode

Important Things to Know

  • Always review your export declarations for any modifiers if you encounter TS1193.
  • Use the TypeScript documentation for further clarity on access modifiers and export rules.

By understanding the rules surrounding export declarations in TypeScript, you can effectively avoid errors like TS1193: An export declaration cannot have modifiers. Remember to structure your code correctly by separating export statements from access modifiers to ensure smooth development processes.

Feel free to reach out or follow along for more articles on TypeScript and helpful insights!

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay