What if you could use types without bloating your app with extra code?
Why Type-only imports and exports in Typescript? - Purpose & Use Cases
Imagine you have a big TypeScript project with many files. You want to use some types from another file, but you also accidentally import code that you don't need at runtime.
This causes your final JavaScript files to be bigger and slower because unnecessary code is included. It also makes your project harder to maintain and can cause confusing errors.
Type-only imports and exports let you bring in just the types without adding any extra code to your final program. This keeps your JavaScript clean and fast while still using TypeScript's powerful type checking.
import { User } from './models'; // User is a type, but this imports code too
import type { User } from './models'; // Only imports the type, no extra code
You can write safer code with types while keeping your final app small and efficient.
When building a web app, you can import types for user data without accidentally including the whole user logic, making your app load faster.
Manual imports can add unwanted code to your app.
Type-only imports keep your JavaScript clean by importing just types.
This improves app size, speed, and maintainability.
Practice
import type in TypeScript?Solution
Step 1: Understand the role of
This syntax imports only type information, not actual code that runs.import typeStep 2: Compare with normal imports
Normal imports bring in both types and runtime code, butimport typeexcludes runtime code.Final Answer:
To import only types without including any runtime code. -> Option DQuick Check:
Type-only imports = import type [OK]
import type brings types only, no code. [OK]- Thinking
import typeimports runtime code - Confusing
import typewith normal import - Using
import typeto export types
Solution
Step 1: Identify correct export type syntax
TypeScript usesexport type { ... }to export only types.Step 2: Check other options
export { MyType } from './types'; (export { MyType } from './types';) exports both types and values. Options B and C are invalid syntax.Final Answer:
export type { MyType } from './types'; -> Option BQuick Check:
Type-only export = export type [OK]
export type {} to export types only. [OK]- Using normal export instead of
export type - Writing invalid syntax like
export only type - Confusing import syntax with export syntax
import type { User } from './models';
function greet(user: User) {
console.log(`Hello, ${user.name}`);
}
greet({ name: "Alice" });
What will happen when this code is compiled and run?
Solution
Step 1: Understand
Theimport typeusageimport typeimports only type info, which is erased at runtime.Step 2: Check runtime behavior
The function usesUseronly as a type for parameter. At runtime, it expects an object with anameproperty.Final Answer:
It will print 'Hello, ' followed by the user's name at runtime. -> Option AQuick Check:
Type-only import works for typing, no runtime error [OK]
- Expecting runtime import of types
- Thinking
import typecauses runtime errors - Confusing compile-time types with runtime values
import type { Config } from './config';
export { Config };Solution
Step 1: Check import and export compatibility
Types imported withimport typemust be exported withexport type.Step 2: Identify correct export syntax
Using normalexport { Config }tries to export a runtime value, which does not exist.Final Answer:
You must useexport type { Config }to export the type correctly. -> Option CQuick Check:
Type-only export matches type-only import [OK]
import type with export type. [OK]- Exporting type-only imports as normal exports
- Ignoring that types do not exist at runtime
- Confusing semicolon errors with syntax errors
shapes.ts exporting types and values:export type Circle = { radius: number };
export const PI = 3.14;You want to import only the type
Circle in another file without importing PI. Which is the best way?Solution
Step 1: Understand import type behavior
import typeimports only types, no runtime values likePI.Step 2: Check options for importing only
import type { Circle } from './shapes'; imports only the typeCircleCirclewithout bringing inPI.Final Answer:
import type { Circle } from './shapes'; -> Option AQuick Check:
Useimport typeto avoid importing values [OK]
import type to import only types, no values. [OK]- Using normal import and importing unwanted values
- Trying to import values with
import type - Importing both types and values when only types are needed
Ready to prove it?
Pass the test and this pattern moves unknown
