Type-only imports and exports let you use types without adding extra code when your program runs. This keeps your code clean and fast.
Type-only imports and exports in Typescript
import type { TypeName } from './file'; export type { TypeName };
Use import type to import only types, not code.
Use export type to export only types from a file.
User type from another file. No code is imported.import type { User } from './userTypes';
User type so other files can use it.export type { User };Config is imported only as a type to check the function parameter.import type { Config } from './config'; function setup(config: Config) { // use config type here }
This program imports only the User type from userTypes.ts. It uses the type to check the parameter of the greet function. The program prints a greeting message.
/* userTypes.ts */ export type User = { name: string; age: number; }; /* main.ts */ import type { User } from './userTypes'; function greet(user: User) { console.log(`Hello, ${user.name}!`); } const person = { name: 'Alice', age: 30 }; greet(person);
Type-only imports do not add any code to the final JavaScript output.
They help keep your code smaller and faster by avoiding unnecessary imports.
If you try to use a type-only import as a value, TypeScript will give an error.
Type-only imports and exports let you use types without importing code.
Use import type and export type to keep your code clean.
This helps your program run faster and stay smaller.
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
