Type-only imports and exports
📖 Scenario: You are working on a TypeScript project where you want to keep your code clean and efficient. Sometimes, you only need to use types from other files without importing the actual code. This helps reduce the final JavaScript size and avoid unnecessary imports.
🎯 Goal: You will create two TypeScript files. In the first file, you will define a type. In the second file, you will import that type using a type-only import and then export it again as a type-only export. Finally, you will print a message to confirm the type usage.
📋 What You'll Learn
Create a type called
User in types.tsUse a type-only import to import
User in main.tsUse a type-only export to export
User from main.tsPrint a confirmation message using
console.log💡 Why This Matters
🌍 Real World
Type-only imports and exports are useful in large TypeScript projects to keep the compiled JavaScript small and avoid importing code that is only needed for type checking.
💼 Career
Many professional TypeScript developers use type-only imports and exports to optimize their code and improve maintainability, especially in frontend and backend projects.
Progress0 / 4 steps