Question
What is the difference between export and export default in TypeScript?
In many tutorials, classes are exported, but my code does not compile unless I add the default keyword before the class export.
For example, this does not work in my project:
export class MyClass {
collection = [1, 2, 3];
}
But this does work:
export default class MyClass {
collection = [1, 2, 3];
}
The compiler error is:
error TS1192: Module '"src/app/MyClass"' has no default export.
Why does this happen, and how should export and export default be used correctly in TypeScript?
Short Answer
By the end of this page, you will understand the difference between named exports and default exports in TypeScript, how they affect import syntax, why the TS1192 error appears, and how to choose the right export style in real code.
Concept
TypeScript modules can export values in two common ways:
- Named export using
export - Default export using
export default
The important difference is how the value must be imported.
Named export
When you write:
export class MyClass {
collection = [1, 2, 3];
}
MyClass is a named export. That means other files must import it by its exact exported name using curly braces:
import { MyClass } from './MyClass';
Default export
When you write:
export default class MyClass {
collection = [1, 2, 3];
}
The module has a default export. That means other files can import it :
Mental Model
Think of a module like a box of tools.
- A named export is like putting labels on specific tools inside the box:
hammer,wrench,screwdriver. - A default export is like saying: "If you take just one main thing from this box, take this one."
Named export
You must ask for the exact labeled tool:
import { MyClass } from './MyClass';
Default export
You are taking the module's main item, so no label lookup is needed:
import MyClass from './MyClass';
If you ask for the default item when the box only contains labeled tools, TypeScript complains. That is what your error means.
Syntax and Examples
Core syntax
Named export
// MyClass.ts
export class MyClass {
collection = [1, 2, 3];
}
Import it like this:
import { MyClass } from './MyClass';
const item = new MyClass();
console.log(item.collection);
Default export
// MyClass.ts
export default class MyClass {
collection = [1, 2, 3];
}
Import it like this:
import MyClass from './MyClass';
const item = new ();
.(item.);
Step by Step Execution
Consider this example:
// MyClass.ts
export class MyClass {
collection = [1, 2, 3];
}
// app.ts
import { MyClass } from './MyClass';
const obj = new MyClass();
console.log(obj.collection);
What happens step by step
1. MyClass.ts defines a class
export class MyClass {
This creates a class named MyClass.
2. export makes it available to other files
Because it uses export, this class becomes a named export of the module.
3. app.ts imports the named export
Real World Use Cases
1. Exporting utility functions
Named exports are common in utility files:
export function formatDate(date: Date) {
return date.toISOString();
}
export function parseDate(value: string) {
return new Date(value);
}
This makes it easy to import only what you need:
import { formatDate } from './dateUtils';
2. Exporting one main component or class
Default exports are often used when a file has one primary thing:
export default class UserService {
getAllUsers() {
return ['Ava', 'Noah'];
}
}
Imported like this:
Real Codebase Usage
In real projects, the choice between named and default exports is usually based on consistency and team preference.
Common named export patterns
Utility modules
export function isEmail(value: string) {
return value.includes('@');
}
export function isPasswordStrong(value: string) {
return value.length >= 8;
}
Validation helpers
export function validateUserName(name: string) {
if (!name.trim()) {
throw new Error('Name is required');
}
}
Re-exporting from index files
export { MyClass } ;
{ } ;
Common Mistakes
1. Importing a named export as if it were default
Broken code:
// MyClass.ts
export class MyClass {}
// app.ts
import MyClass from './MyClass';
Why it fails:
MyClassis a named export- the import expects a default export
Fix:
import { MyClass } from './MyClass';
2. Importing a default export with curly braces
Broken code:
// MyClass.ts
export default class MyClass {}
// app.ts
import { MyClass } from './MyClass';
Fix:
import ;
Comparisons
| Feature | export | export default |
|---|---|---|
| Export type | Named export | Default export |
| Import syntax | import { MyClass } from './MyClass' | import MyClass from './MyClass' |
| Uses curly braces | Yes | No |
| Must match exported name | Yes | No |
| Can a file have many of these? | Yes | Only one default export |
| Common use | Utilities, helpers, multiple exports | One main class, component, or config |
Named export vs default export example
Cheat Sheet
Quick rules
export= named exportexport default= default export- Named exports are imported with
{} - Default exports are imported without
{} - A file can have many named exports
- A file can have only one default export
Syntax
Named export
export class MyClass {}
import { MyClass } from './MyClass';
Default export
export default class MyClass {}
import MyClass from './MyClass';
Multiple named exports
export const a = 1;
export const b = 2;
{ a, b } ;
FAQ
Why do I get "Module has no default export" in TypeScript?
Because you are importing a module as if it had a default export, but the file only exports named values.
Is export default required for classes in TypeScript?
No. Classes can be exported either as named exports or as a default export. The import syntax must match.
Why do some tutorials use export default and others use export?
Because both are valid. Different projects and teachers prefer different module styles.
Can I have both export default and named exports in the same file?
Yes. A file can have one default export and any number of named exports.
Which is better: named exports or default exports?
Neither is always better. Named exports are often preferred in larger codebases for clarity. Default exports are convenient when a file has one main value.
Can I rename imports?
Yes.
For named exports:
import { MyClass as CustomName } from './MyClass';
For default exports:
import CustomName ;
Mini Project
Description
Build a small TypeScript module setup with one file exporting a class and helper function, then import them correctly in another file. This project demonstrates the difference between named exports and default exports in a realistic but simple way.
Goal
Create two modules and import their exports correctly without triggering TS1192.
Requirements
- Create one file that uses a named export for a class.
- Create one file that uses a default export for a configuration object.
- Import both correctly in a main file.
- Instantiate the class and print one property.
- Read and print a value from the default-exported configuration.
Keep learning
Related questions
@Directive vs @Component in Angular: Differences, Use Cases, and When to Use Each
Learn the difference between @Directive and @Component in Angular, including use cases, examples, and when to choose each.
Angular (change) vs (ngModelChange): What’s the Difference?
Learn the difference between Angular (change) and (ngModelChange), when each fires, and which one to use in forms and inputs.
Angular @ViewChild Returning Undefined: Lifecycle, Child Components, and Fixes
Learn why Angular @ViewChild can be undefined, when it becomes available, and how to access child components correctly using lifecycle hooks.