typescript
/

TypeScript Modules

Last Sync: Today

On this page

10
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

typescript

TypeScript Modules

What are Modules?

Modules allow you to split code into separate files and reuse functionality using import and export statements.

Export Example

TypeScriptRead-only
1
export const name: string = 'TypeScript';

export function greet() {
  console.log('Hello');
}

Import Example

TypeScriptRead-only
1
import { name, greet } from './module';

greet();

Default Export

TypeScriptRead-only
1
export default function greet() {
  console.log('Hello');
}

Import Default

TypeScriptRead-only
1
import greet from './module';

Alias Import

TypeScriptRead-only
1
import { greet as sayHello } from './module';

Module Benefits

  • Better code organization
  • Reusability
  • Maintainability
  • Scalable architecture

Best Practices

  • Use named exports for multiple items
  • Use default export for main functionality
  • Keep modules small and focused
  • Organize files properly

Common Mistakes

  • Mixing default and named exports incorrectly
  • Large and unorganized modules
  • Incorrect import paths
  • Circular dependencies

Conclusion

Modules are essential for structuring TypeScript applications, enabling clean, reusable, and maintainable code.

Try it yourself

export const x = 10;
import { x } from './file';

Test Your Knowledge

Q1
of 3

Modules used for?

A
Styling
B
Code organization
C
Routing
D
API
Q2
of 3

Export keyword?

A
import
B
export
C
module
D
require
Q3
of 3

Import keyword?

A
use
B
require
C
import
D
include

Frequently Asked Questions

What is module?

A file with reusable code.

Export keyword?

Used to share code.

Import keyword?

Used to use code from other files.

Previous

ts conditional types

Next

ts namespaces

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.