> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript Export Assignment

TypeScript's export assignment (`export =`) is a module syntax designed to model the CommonJS `module.exports` and AMD `return` patterns. It specifies a single entity or expression as the exact exported value of a module, replacing the module object entirely.

Unlike standard ECMAScript modules (ESM) where `export default` creates a specific `default` export binding, the `export =` syntax binds the exported value directly to the module root.

## Syntax

The export assignment is declared using the `export =` keywords followed by any valid expression. This can be an identifier, an inline object, an anonymous class, or a primitive value.

```typescript theme={"dark"}
// calculator.ts
class Calculator {
    add(a: number, b: number): number {
        return a + b;
    }
}

export = Calculator;
```

```typescript theme={"dark"}
// config.ts
export = { 
    port: 8080, 
    host: "localhost" 
};
```

When compiled to CommonJS, the TypeScript compiler emits this directly as a `module.exports` assignment:

```javascript theme={"dark"}
// Compiled JavaScript (CommonJS)
class Calculator {
    add(a, b) {
        return a + b;
    }
}
module.exports = Calculator;
```

## Import Requirements

Modules exporting a value via `export =` cannot be imported using standard ES6 `import` syntax (e.g., `import Calc from "./calculator"`) under default compiler settings.

By default, TypeScript requires a corresponding, TypeScript-specific import syntax: `import ... = require(...)`.

```typescript theme={"dark"}
// app.ts
import Calculator = require("./calculator");
import config = require("./config");

const calc = new Calculator();
calc.add(2, 3);
```

**Compiler Flags and Default Imports:**
To consume an `export =` assignment using standard ES6 default imports (`import Calculator from "./calculator"`), specific compiler flags in `tsconfig.json` must be utilized:

* `allowSyntheticDefaultImports`: Setting this to `true` instructs the TypeScript *typechecker* to allow the ES6 default import syntax. However, it **does not change the emitted JavaScript**. If the code is compiled to CommonJS and executed in Node.js without a bundler, it will crash at runtime because the emitted JavaScript will attempt to access a `.default` property that does not exist.
* `esModuleInterop`: To safely execute this pattern in CommonJS, this flag must be enabled. It automatically enables `allowSyntheticDefaultImports` for the typechecker *and* instructs the compiler to emit the `__importDefault` runtime helper. This helper ensures the emitted JavaScript correctly resolves the module root at runtime, preventing crashes.

## Technical Constraints

1. **Mutual Exclusivity:** A module utilizing `export =` cannot contain any other ES6 exports (named or default). The export assignment claims the entire module namespace.

```typescript theme={"dark"}
export = { add: (a: number, b: number) => a + b };
export const PI = 3.14; // Error: An export assignment cannot be used in a module with other exported elements.
```

2. **Declaration Merging:** To export multiple entities using `export =` via identifiers, developers must leverage TypeScript's declaration merging (e.g., merging a namespace with a class or function) or export a single aggregate object expression.

```typescript theme={"dark"}
function Logger(msg: string) {}

namespace Logger {
    export const level = "info";
}

export = Logger; // Exports both the callable function and the attached namespace properties
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor TypeScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
