How Do Default Exports Work in TypeScript?

Learn How Do Default Exports Work in TypeScript? with practical examples, explanations, best practices, and real-world coding techniques.

Introduction to Default Exports

TypeScript modules allow developers to organize application logic into separate files and share selected declarations through imports and exports. A module might contain a service, utility, component, or class that another part of the application needs to use.

A default export allows a module to expose one primary declaration or value. The importing file can assign its own local name to that default export, making it convenient to consume classes, functions, and other values without having to match the original declaration name.

Default exports are commonly used when a file has one clear main responsibility, such as defining a Course class, creating a configuration object, or providing a primary function. Understanding their syntax and differences from named exports helps you choose a consistent module structure for your application.

1. What Is a Default Export?

A default export marks a value or declaration as the default export of a module. A module can have only one default export declaration, although it can also provide multiple named exports.

Consider a TypeScript file that defines a function for formatting course names:

// format-course.ts

export default function formatCourseName(name: string): string {
  return name.trim().toUpperCase();
}

The default export makes formatCourseName available to importing modules as the module's primary exported function.

// app.ts

import formatCourseName from "./format-course";

console.log(formatCourseName("  TypeScript  "));

Notice that the import does not use curly braces. Curly braces are used for named imports, whereas a default import is written directly after the import keyword.

The import path identifies the module containing the function. The exact path and file extension requirements depend on the project's module configuration, runtime, and build tooling.

2. Default Exports with Functions

Functions are a common choice for default exports when a file primarily provides one operation. This approach works well for utilities, formatting functions, data-processing operations, and other reusable behaviors.

// calculate-total.ts

export default function calculateTotal(
  price: number,
  quantity: number
): number {
  return price * quantity;
}

Another module can import and call the function:

// checkout.ts

import calculateTotal from "./calculate-total";

const total = calculateTotal(250, 3);

console.log(total);

The function's parameter and return types continue to be checked by TypeScript. Default export syntax changes how the declaration is exposed by the module; it does not remove the function's type information.

You can give a default-exported function a descriptive name, as in this example. Named function declarations can also improve stack traces and debugging compared with some anonymous function expressions.

Use this pattern when one function represents the main purpose of a module. If the file provides several equally important operations, named exports may communicate its public interface more clearly.

3. Default Exports with Classes

Classes can be exported by default when the class is the primary declaration in a file. This is a common organization pattern for service classes, domain objects, controllers, and other class-based implementations.

// course.ts

export default class Course {
  constructor(
    public id: number,
    public title: string
  ) {}

  getTitle(): string {
    return this.title;
  }
}

The importing module can choose a local name for the class:

// app.ts

import Course from "./course";

const course = new Course(101, "TypeScript Fundamentals");

console.log(course.id);
console.log(course.getTitle());

Course is the local name chosen by the importing file. The class remains usable as a runtime value, so you can construct instances and call its methods.

Another module could import the same default export under a different local name, such as LearningCourse. That flexibility can be useful, although keeping the imported name consistent with the class's actual purpose generally improves readability.

When a module primarily defines one class, a default export can make the import concise. For modules containing several related classes, consider named exports or a dedicated public entry point.

4. Exporting Values as Default

You do not have to use the export default syntax directly on a function or class declaration. You can declare a value first and then export it using export default.

// app-config.ts

interface AppConfig {
  appName: string;
  apiBaseUrl: string;
  enableLogging: boolean;
}

const appConfig: AppConfig = {
  appName: "IgKnight",
  apiBaseUrl: "/api",
  enableLogging: true
};

export default appConfig;

The configuration object can be imported by another module:

// app.ts

import appConfig from "./app-config";

console.log(appConfig.appName);
console.log(appConfig.apiBaseUrl);
console.log(appConfig.enableLogging);

This pattern is useful for configuration objects, constants, factory results, and other values that represent a module's primary export.

The AppConfig interface describes the object during type checking, while appConfig is the actual runtime value. The interface itself does not exist as a JavaScript object after ordinary TypeScript compilation.

Exporting an object does not automatically make it immutable. If consumers must not change a configuration value, use appropriate readonly types or another design that protects the data from unintended modification.

5. Anonymous Functions as Default Exports

TypeScript also allows a function expression to be exported directly as the default value of a module. This can be useful for compact modules whose main export is a single function.

// normalize-name.ts

export default function (name: string): string {
  return name.trim().toLowerCase();
}

The importing file provides its own local name:

// app.ts

import normalizeName from "./normalize-name";

console.log(normalizeName("  ASHA  "));

The function does not need a declared name in the exporting file because the importing module assigns a local binding to the default export.

However, anonymous default function declarations can be less descriptive when reading debugging output or inspecting the source. A named default function is often a better choice when the operation has a meaningful name that should be recognized throughout the codebase.

6. Choosing a Name When Importing a Default Export

One defining characteristic of a default export is that its importing file chooses the local import name. The local name does not have to match the original function or class name.

// greeting.ts

export default function greet(name: string): string {
  return "Hello, " + name;
}
// app.ts

import greetUser from "./greeting";
import createGreeting from "./greeting";

console.log(greetUser("Asha"));
console.log(createGreeting("Rahul"));

Both imports refer to the same module's default export, but they use different local names. They are not separate implementations; they are separate bindings to the same exported function.

Importing the same declaration multiple times under different names is usually unnecessary in one file. The example illustrates the syntax, while a real application should normally select one clear, descriptive name.

Prefer local names that reflect the function's purpose. Although arbitrary names are valid, choosing names that are unrelated to the exported value can confuse readers and make refactoring more difficult.

7. Using Named and Default Exports Together

A module can contain one default export and several named exports. This is useful when a file provides a primary class or function alongside supporting constants and utilities.

// course.ts

export const platformName = "IgKnight";

export function getCourseCount(courses: string[]): number {
  return courses.length;
}

export default class Course {
  constructor(
    public id: number,
    public title: string
  ) {}
}

You can import the default class and named declarations in the same statement:

// app.ts

import Course, {
  platformName,
  getCourseCount
} from "./course";

const course = new Course(101, "TypeScript");

console.log(platformName);
console.log(course.title);
console.log(getCourseCount(["HTML", "CSS", "TypeScript"]));

The default import appears before the named imports. Curly braces surround the named exports, while the default class is imported without braces.

This arrangement can be effective when a file has a natural primary export. However, do not add a default export just to provide a second way of importing a declaration when that does not improve the module's public API.

8. Re-Exporting a Default Export

A module can re-export the default export of another module. This allows you to expose an existing declaration through a different file without duplicating its implementation.

// course.ts

export default class Course {
  constructor(
    public id: number,
    public title: string
  ) {}
}
// models.ts

export { default as Course } from "./course";

Another module can now import Course as a named export from models.ts:

// app.ts

import { Course } from "./models";

const course = new Course(101, "TypeScript");

console.log(course.title);

The syntax export { default as Course } imports the source module's default export and exposes it as a named export called Course.

This pattern is useful for barrel files, shared model entry points, and libraries that want to standardize their public exports. It can also make a module's external interface more consistent even when its internal files use different export styles.

A re-export does not normally duplicate the original implementation. It provides another access path to the exported declaration, with module-loading behavior determined by the project's runtime and build tools.

9. Default Exports and TypeScript Types

TypeScript supports default exports of runtime values such as functions, classes, and objects. Interfaces and type aliases require particular care because they exist only in the type system and are erased from ordinary JavaScript output.

A common and straightforward pattern is to define a type using a named export, then import it using import type:

// course-model.ts

export interface Course {
  id: number;
  title: string;
}

export type CourseStatus = "draft" | "published";
// course-service.ts

import type { Course, CourseStatus } from "./course-model";

export default function getCourseStatus(
  course: Course
): CourseStatus {
  return course.title.length > 0 ? "published" : "draft";
}

The function is the module's default runtime export, while Course and CourseStatus are named type exports from the model module.

Separating shared model types from runtime implementation makes the code easier to understand. It also clarifies which imports are required only for checking and which declarations must exist when the application runs.

In general, use named exports for reusable interfaces and type aliases unless a particular module design gives a compelling reason to use a different type-export pattern. Keep compile-time types distinct from runtime values.

10. Default Exports vs Named Exports

Default exports and named exports both allow modules to expose declarations, but they have different import syntax and naming behavior.

Feature Default Export Named Export
Import syntax Without braces With braces
Local import name Chosen by the importer Matches the export unless aliased
Exports per module One default export Multiple named exports
Typical usage One primary declaration Several explicit declarations
Aliasing The import chooses a local name Use as to rename the import
// exports.ts

export const version = "1.0";

export default function greet(name: string): string {
  return "Hello, " + name;
}

// app.ts

import greetUser, { version } from "./exports";

console.log(greetUser("Asha"));
console.log(version);

A default export is convenient when one declaration represents the module's main purpose. Named exports are useful for explicit interfaces containing multiple independent declarations.

Neither approach is universally better. The right choice depends on the module's responsibility, the project's conventions, and how its consumers should discover and use the available functionality.

11. Common Default Export Mistakes

Using curly braces for a default import

A default export is imported without braces. If the source uses export default, import Course from "./course" is the usual form. Curly braces are used for named exports.

Using multiple default exports in one module

A module cannot declare multiple independent default exports. Use named exports for additional functions, constants, types, or classes that must be shared from the same file.

Confusing local names with exported names

Default imports may use any valid local name, while named imports must match the exported name unless an alias is provided. Check the source declaration when resolving import errors.

Using a type as a runtime object

Interfaces and type aliases are erased from ordinary JavaScript output. Do not expect a type-only declaration to exist as a runtime value simply because it is part of a TypeScript module.

Choosing unclear import names

Although the importer chooses the local name for a default export, using unrelated names can make code harder to read. Prefer a name that communicates what the imported declaration represents.

Ignoring module resolution

Correct export syntax does not guarantee that a module path works in every runtime. Verify file paths, extensions, module settings, and bundler behavior when an import cannot be resolved.

Best Practices for Default Exports

  • Use a default export when one function, class, or value is clearly the main purpose of a module.
  • Use named exports when a module exposes several related declarations that consumers should identify explicitly.
  • Keep the local name of an imported default export meaningful and consistent with its purpose.
  • Use named exports for shared interfaces and type aliases when that makes the public API clearer.
  • Use re-exports to create intentional public entry points without duplicating implementations.
  • Avoid adding both default and named exports unless both forms serve a genuine purpose.
  • Keep module paths and extensions aligned with the application's runtime and build tooling.
  • Separate type-only declarations from runtime values so import behavior remains understandable.

Conclusion

Default exports allow a TypeScript module to expose one primary declaration or value that importing files can reference under their own local names. They work well for functions, classes, configuration objects, and other values that represent the main responsibility of a file.

Understanding the difference between default and named imports, combining export styles, and re-exporting declarations helps you organize modules with clear public APIs. Choose an export convention that suits the module's purpose, keep import names meaningful, and ensure module configuration matches the target runtime or build system.

Code Examples

Default Export with a Function

typescript
// format-course.ts
export default function formatCourseName(name: string): string {
  return name.trim().toUpperCase();
}

// app.ts
import formatCourseName from "./format-course";

console.log(formatCourseName("  TypeScript  "));
    

The function is the module’s default export, so the importing file can use it without curly braces and choose a local name.

Default Export with a Class

typescript
// course.ts
export default class Course {
  constructor(
    public id: number,
    public title: string
  ) {}

  getTitle(): string {
    return this.title;
  }
}

// app.ts
import Course from "./course";

const course = new Course(101, "TypeScript");

console.log(course.getTitle());
    

A class can be exported by default when it is the primary declaration of the module. The import provides the runtime class used to create instances.

Default Export with a Configuration Object

typescript
// app-config.ts
interface AppConfig {
  appName: string;
  enableLogging: boolean;
}

const appConfig: AppConfig = {
  appName: "IgKnight",
  enableLogging: true
};

export default appConfig;

// app.ts
import appConfig from "./app-config";

console.log(appConfig.appName);
console.log(appConfig.enableLogging);
    

An object can be declared separately and then exported as the default value of a module.

Default and Named Exports Together

typescript
// course.ts
export const platformName = "IgKnight";

export function getCourseCount(courses: string[]): number {
  return courses.length;
}

export default class Course {
  constructor(
    public id: number,
    public title: string
  ) {}
}

// app.ts
import Course, {
  platformName,
  getCourseCount
} from "./course";

const course = new Course(101, "TypeScript");

console.log(platformName);
console.log(course.title);
console.log(getCourseCount(["HTML", "CSS"]));
    

A module can have one default export and multiple named exports. The default declaration is imported without braces, while named exports use braces.

Re-Exporting a Default as a Named Export

typescript
// course.ts
export default class Course {
  constructor(
    public id: number,
    public title: string
  ) {}
}

// models.ts
export { default as Course } from "./course";

// app.ts
import { Course } from "./models";

const course = new Course(101, "TypeScript");

console.log(course.title);
    

The re-export gives the source module’s default export a named public entry, allowing consumers to import it with curly braces.

Choosing a Local Import Name

typescript
// greeting.ts
export default function greet(name: string): string {
  return "Hello, " + name;
}

// app.ts
import greetUser from "./greeting";

console.log(greetUser("Asha"));
    

The name used for a default import is selected by the importing module. Descriptive local names make the code easier to understand.

Pro Tip

Use a default export when a file has one clear primary function, class, or value. Import it without curly braces and choose a descriptive local name. Use named exports for additional utilities and shared types, and keep the module’s public API consistent instead of mixing export styles without a clear reason.

More TypeScript lessons

🎨 IgKnight Docs

Master HTML, CSS, JS, Python, .NET and more with easy-to-follow lessons and practical code examples.

© 2025 IgKnight Learning Platform. All rights reserved.