A dynamic import is a way to load modules asynchronously at runtime using the <code>import()</code> function, allowing code to be split and loaded only when needed.
Click to reveal answer
intermediate
How do you specify types when using dynamic imports in TypeScript?
You can specify types by using import() with a type assertion or by importing the module type separately and then asserting the imported module's shape.
Click to reveal answer
intermediate
Why use dynamic imports with types instead of static imports?
Dynamic imports help reduce initial load time by loading code only when needed, and using types ensures type safety and better developer experience with autocompletion and error checking.
Click to reveal answer
beginner
Show a simple example of a dynamic import with type in TypeScript.
What happens if the dynamically imported module does not match the expected type?
TypeScript will show a type error during development if the module does not match the expected type, helping catch mistakes early before running the code.
Click to reveal answer
What syntax is used for dynamic imports in TypeScript?
Ainclude('./module')
Brequire('./module')
Cimport './module'
Dimport('./module')
✗ Incorrect
Dynamic imports use the import() function syntax to load modules asynchronously.
How can you ensure type safety when using dynamic imports?
AUse type assertions or import types separately
BUse <code>any</code> type
CIgnore types for dynamic imports
DUse <code>require()</code> instead
✗ Incorrect
Type assertions or importing types separately help ensure type safety with dynamic imports.
What is a benefit of dynamic imports?
AMake code run synchronously
BLoad code only when needed
CDisable type checking
DIncrease initial bundle size
✗ Incorrect
Dynamic imports allow loading code only when needed, improving performance.
Which keyword is NOT used with dynamic imports?
Arequire
Bimport
Cawait
Das
✗ Incorrect
require is CommonJS syntax, not used for dynamic imports in TypeScript.
What does this code do? const mod = await import('./mod') as { foo: () => void };
AImports './mod' synchronously
BStatically imports './mod' without types
CDynamically imports './mod' and asserts it has a foo function
DThrows an error always
✗ Incorrect
This code dynamically imports the module and asserts it has a function named foo.
Explain how to use dynamic imports with types in TypeScript and why it is useful.
Think about loading code only when needed and keeping type checks.
You got /3 concepts.
Describe a simple example of a dynamic import with a type assertion in TypeScript.
Show how to load a module and tell TypeScript what shape it has.
You got /4 concepts.
Practice
(1/5)
1. What is the main benefit of using dynamic imports with types in TypeScript?
easy
A. They make the code run faster by compiling to machine code.
B. They load modules only when needed, improving app speed and type safety.
C. They automatically convert JavaScript to TypeScript.
D. They prevent any runtime errors without writing tests.
Solution
Step 1: Understand dynamic imports
Dynamic imports load code only when it is needed, not all at once.
Step 2: Understand types with dynamic imports
Using typeof import() adds type safety, helping catch errors early.
Final Answer:
They load modules only when needed, improving app speed and type safety. -> Option B
Quick Check:
Dynamic imports + types = faster and safer code [OK]
A. Type annotation is incorrect; should not use typeof import().
B. Cannot call console.log inside async code.
C. Missing 'await' before import, so mod is a Promise, not the module.
D. Module './utils' does not exist.
Solution
Step 1: Check import usage
Dynamic import returns a Promise, so it must be awaited to get the module.
Step 2: Identify error cause
Without await, mod is a Promise, so calling mod.helper() causes an error.
Final Answer:
Missing 'await' before import, so mod is a Promise, not the module. -> Option C
Quick Check:
Dynamic import needs await to get module [OK]
Hint: Always await dynamic imports to get the module object [OK]
Common Mistakes:
Forgetting to await dynamic import
Misusing type annotation syntax
Assuming import returns module directly
5. You want to dynamically import a module './config' that exports a default object with a property apiUrl. How do you correctly type and access apiUrl using dynamic imports in TypeScript?
hard
A. const config: typeof import('./config') = await import('./config'); console.log(config.default.apiUrl);
B. const config = await import('./config'); console.log(config.apiUrl);
C. const config: typeof import('./config') = await import('./config'); console.log(config.apiUrl);
D. const config = await import('./config'); console.log(config.default.apiUrl);
Solution
Step 1: Understand default exports in dynamic imports
When a module exports default, it is accessed via the default property on the imported module object.
Step 2: Use type annotation and access property
Typing with typeof import() is correct, and to get apiUrl, use config.default.apiUrl.