A type-only import brings in types from another file without including any JavaScript code in the final output. It uses the <code>import type</code> syntax.
Click to reveal answer
beginner
How do you write a type-only export in TypeScript?
You use export type before the type or interface you want to export, so it only exports the type information without any runtime code.
Click to reveal answer
intermediate
Why use type-only imports and exports?
They help keep the JavaScript output clean by removing unnecessary code, improving performance and avoiding circular dependencies at runtime.
Click to reveal answer
intermediate
What happens if you import a type normally instead of using <code>import type</code>?
TypeScript may include extra JavaScript code in the output, which can increase bundle size and cause runtime errors if the imported item is not a value.
Click to reveal answer
beginner
Show an example of a type-only import and a type-only export.
Example:<br><pre>import type { User } from './models';
export type ID = string | number;</pre>
Click to reveal answer
Which syntax imports only types without including JavaScript code?
Aimport { MyType } from './file';
Bimport type { MyType } from './file';
Cexport type { MyType } from './file';
Dexport { MyType } from './file';
✗ Incorrect
Using import type imports only the type information and excludes JavaScript code.
What keyword is used to export only types in TypeScript?
Aexport type
Bexport interface
Cexport const
Dexport class
✗ Incorrect
export type exports only type information without runtime code.
Why might you want to use type-only imports?
ATo reduce JavaScript bundle size
BTo import runtime values
CTo execute code immediately
DTo create side effects
✗ Incorrect
Type-only imports avoid including runtime code, reducing bundle size.
What happens if you import a type without import type and the type is not a value?
AThe program crashes immediately
BThe import is ignored
CExtra JavaScript code may be included
DThe type is converted to a value
✗ Incorrect
Normal imports may include runtime code even if only types are needed, causing extra code.
Which of these is a correct type-only export?
Aexport interface User {}
Bexport const User = { name: 'Alice' };
Cexport class User {}
Dexport type User = { name: string };
✗ Incorrect
export type exports only the type definition without runtime code.
Explain what type-only imports and exports are and why they are useful.
Think about how TypeScript separates types from JavaScript code.
You got /3 concepts.
Write a small example showing how to import a type only and export a type only in TypeScript.
Use <code>import type</code> and <code>export type</code> keywords.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using import type in TypeScript?
easy
A. To export types from a module.
B. To import both types and runtime values together.
C. To import only runtime values and ignore types.
D. To import only types without including any runtime code.
Solution
Step 1: Understand the role of import type
This syntax imports only type information, not actual code that runs.
Step 2: Compare with normal imports
Normal imports bring in both types and runtime code, but import type excludes runtime code.
Final Answer:
To import only types without including any runtime code. -> Option D
Quick Check:
Type-only imports = import type [OK]
Hint: Remember: import type brings types only, no code. [OK]
Common Mistakes:
Thinking import type imports runtime code
Confusing import type with normal import
Using import type to export types
2. Which of the following is the correct syntax to export only types from a TypeScript module?
easy
A. export { MyType } from './types';
B. export type { MyType } from './types';
C. export import type MyType from './types';
D. export only type MyType from './types';
Solution
Step 1: Identify correct export type syntax
TypeScript uses export 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 B
Quick Check:
Type-only export = export type [OK]
Hint: Use export type {} to export types only. [OK]
Common Mistakes:
Using normal export instead of export type
Writing invalid syntax like export only type
Confusing import syntax with export syntax
3. Consider the following code snippet:
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?
medium
A. It will print 'Hello, ' followed by the user's name at runtime.
B. It will cause a runtime error because User is not imported as code.
C. It will fail to compile because import type cannot be used with functions.
D. It will import the full User class including methods.
Solution
Step 1: Understand import type usage
The import type imports only type info, which is erased at runtime.
Step 2: Check runtime behavior
The function uses User only as a type for parameter. At runtime, it expects an object with a name property.
Final Answer:
It will print 'Hello, ' followed by the user's name at runtime. -> Option A
Quick Check:
Type-only import works for typing, no runtime error [OK]
Hint: Type-only imports vanish at runtime but keep type safety. [OK]
Common Mistakes:
Expecting runtime import of types
Thinking import type causes runtime errors
Confusing compile-time types with runtime values
4. Identify the error in this TypeScript code:
import type { Config } from './config';
export { Config };
medium
A. You cannot export a type that was imported with import type.
B. The import type statement is missing a semicolon.
C. You must use export type { Config } to export the type correctly.
D. There is no error; this code works fine.
Solution
Step 1: Check import and export compatibility
Types imported with import type must be exported with export type.
Step 2: Identify correct export syntax
Using normal export { Config } tries to export a runtime value, which does not exist.
Final Answer:
You must use export type { Config } to export the type correctly. -> Option C
Quick Check:
Type-only export matches type-only import [OK]
Hint: Match import type with export type. [OK]
Common Mistakes:
Exporting type-only imports as normal exports
Ignoring that types do not exist at runtime
Confusing semicolon errors with syntax errors
5. You have a module 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?
hard
A. import type { Circle } from './shapes';
B. import { Circle } from './shapes';
C. import { Circle, PI } from './shapes';
D. import type { Circle, PI } from './shapes';
Solution
Step 1: Understand import type behavior
import type imports only types, no runtime values like PI.
Step 2: Check options for importing only Circle
import type { Circle } from './shapes'; imports only the type Circle without bringing in PI.
Final Answer:
import type { Circle } from './shapes'; -> Option A
Quick Check:
Use import type to avoid importing values [OK]
Hint: Use import type to import only types, no values. [OK]
Common Mistakes:
Using normal import and importing unwanted values
Trying to import values with import type
Importing both types and values when only types are needed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.