Type-only imports and exports in Typescript - Time & Space Complexity
We want to understand how using type-only imports and exports affects the time it takes for TypeScript to process code.
Specifically, we ask: does importing or exporting only types change how the program runs as it grows?
Analyze the time complexity of the following code snippet.
// Importing only types
import type { User, Product } from './models';
// Exporting only types
export type { User, Product };
function processUser(user: User) {
// some logic here
}
This code imports and exports only types without bringing in any runtime code.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: There are no loops or repeated operations here because type-only imports do not generate runtime code.
- How many times: The operations happen once during compile time for type checking, not during program execution.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Small number of type checks, no runtime cost |
| 100 | More type checks, still no runtime cost |
| 1000 | Many type checks, but runtime execution remains unaffected |
Pattern observation: The compile-time work grows with the number of types, but runtime execution time stays the same.
Time Complexity: O(1)
This means the runtime cost does not grow with the number of type-only imports or exports because they are removed before running.
[X] Wrong: "Importing many types will slow down my program when it runs."
[OK] Correct: Type-only imports are erased during compilation, so they do not add any runtime cost or slow down the program.
Understanding how type-only imports affect performance shows you know the difference between compile-time and runtime, a key skill in TypeScript development.
"What if we changed type-only imports to regular imports? How would the time complexity change at runtime?"
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
