TypeScript
/Beginner
ES Modules in TypeScript
Definition
The official standard format to package JavaScript code for reuse. TypeScript relies heavily on ES Modules (`import` and `export`) to share types, interfaces, and logic across files.
Explain Like I'm New
Before ES Modules, all JS files were just dumped into a giant global bucket, causing variables to overwrite each other. ES Modules give every file its own private room. Nothing leaves the room unless you explicitly `export` it, and nothing enters the room unless you explicitly `import` it.
Real World Example
Creating a `types.ts` file that holds all your global interfaces, and importing them into your React components to keep the component files clean.
Common Use Cases
- •File separation
- •Code reusability
- •Avoiding global namespace pollution
Interactive Example
Loading Code Playground...
Interview Questions
basic
- What keywords are used to share code between files?
intermediate
- Does exporting an `interface` add any code to your final JavaScript bundle?
advanced
- What is `import type`?