Bird
Raised Fist0
Typescriptprogramming~20 mins

Dynamic imports with types in Typescript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Dynamic Import Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this dynamic import with type assertion?
Consider this TypeScript code that dynamically imports a module and uses a type assertion. What will be logged to the console?
Typescript
async function loadModule() {
  const module = await import('./mathUtils') as { add: (a: number, b: number) => number };
  console.log(module.add(2, 3));
}
loadModule();
ASyntaxError
BTypeError at runtime
Cundefined
D5
Attempts:
2 left
💡 Hint
Think about what the type assertion does and what the imported module exports.
❓ Predict Output
intermediate
2:00remaining
What error does this dynamic import with wrong type cause?
What error will this TypeScript code produce at runtime?
Typescript
async function loadModule() {
  const module = await import('./stringUtils') as { multiply: (a: number, b: number) => number };
  console.log(module.multiply(2, 3));
}
loadModule();
ASyntaxError
B5
CTypeError: module.multiply is not a function
Dundefined
Attempts:
2 left
💡 Hint
The type assertion does not change the actual module content at runtime.
🧠 Conceptual
advanced
1:30remaining
Which statement about dynamic imports with types is true?
Choose the correct statement about using dynamic imports with TypeScript types.
AType assertions help TypeScript check types but do not affect runtime values.
BDynamic imports always return the exact type declared in the import statement.
CType assertions on dynamic imports affect runtime behavior and prevent errors.
DDynamic imports cannot be typed in TypeScript.
Attempts:
2 left
💡 Hint
Think about the difference between compile-time and runtime.
❓ Predict Output
advanced
2:00remaining
What is the output of this dynamic import with conditional type?
What will be logged by this TypeScript code using dynamic import and conditional types?
Typescript
type ModuleType<T extends string> = T extends 'math' ? { add: (a: number, b: number) => number } : { greet: (name: string) => string };

async function load<T extends string>(name: T): Promise<ModuleType<T>> {
  return await import(`./${name}Utils`) as ModuleType<T>;
}

load('math').then(m => console.log(m.add(1, 2)));
ATypeError at runtime
B3
Cundefined
DSyntaxError
Attempts:
2 left
💡 Hint
Look at how the conditional type selects the module shape.
🔧 Debug
expert
2:30remaining
Why does this dynamic import with typescript fail at runtime?
This code tries to dynamically import a module and use a typed function, but it fails at runtime. Why?
Typescript
async function loadAndRun() {
  const mod = await import('./utils') as { default: (x: number) => number };
  console.log(mod(5));
}
loadAndRun();
Amod is the module object, not the default export function, so mod(5) causes TypeError
BType assertion is wrong syntax causing SyntaxError
CThe import path is invalid causing runtime error
Dmod is undefined causing ReferenceError
Attempts:
2 left
💡 Hint
Remember how dynamic imports return module objects with exports as properties.

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

  1. Step 1: Understand dynamic imports

    Dynamic imports load code only when it is needed, not all at once.
  2. Step 2: Understand types with dynamic imports

    Using typeof import() adds type safety, helping catch errors early.
  3. Final Answer:

    They load modules only when needed, improving app speed and type safety. -> Option B
  4. Quick Check:

    Dynamic imports + types = faster and safer code [OK]
Hint: Dynamic imports delay loading; types keep code safe [OK]
Common Mistakes:
  • Thinking dynamic imports compile code faster
  • Confusing dynamic imports with automatic type conversion
  • Believing dynamic imports prevent all runtime errors
2. Which of the following is the correct syntax to dynamically import a module named utils with types in TypeScript?
easy
A. const utils: typeof import('utils') = await import('utils');
B. const utils = await import('utils');
C. import utils from 'utils';
D. const utils = import('utils');

Solution

  1. Step 1: Identify dynamic import syntax

    Dynamic imports use await import('module') inside async functions.
  2. Step 2: Add type annotation

    Use const x: typeof import('module') to add types for the imported module.
  3. Final Answer:

    const utils: typeof import('utils') = await import('utils'); -> Option A
  4. Quick Check:

    Dynamic import with type annotation = const utils: typeof import('utils') = await import('utils'); [OK]
Hint: Use 'const x: typeof import() = await import()' [OK]
Common Mistakes:
  • Omitting 'await' in dynamic import
  • Using static import syntax for dynamic import
  • Not adding type annotation for imported module
3. Consider this TypeScript code inside an async function:
const mod: typeof import('./math') = await import('./math');
const result = mod.add(2, 3);
console.log(result);

If ./math exports export function add(a: number, b: number) { return a + b; }, what will be the output?
medium
A. Type error at compile time
B. undefined
C. Runtime error: mod.add is not a function
D. 5

Solution

  1. Step 1: Understand dynamic import with types

    The module is imported dynamically with correct typing, so mod.add is recognized as a function.
  2. Step 2: Calculate the function call

    Calling mod.add(2, 3) returns 5, which is logged.
  3. Final Answer:

    5 -> Option D
  4. Quick Check:

    Dynamic import calls function correctly = 5 [OK]
Hint: Dynamic import with correct type calls function normally [OK]
Common Mistakes:
  • Expecting undefined because of dynamic import
  • Confusing runtime error with type error
  • Forgetting to await the import
4. What is wrong with this TypeScript code snippet?
const mod: typeof import('./utils') = import('./utils');
console.log(mod.helper());
medium
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

  1. Step 1: Check import usage

    Dynamic import returns a Promise, so it must be awaited to get the module.
  2. Step 2: Identify error cause

    Without await, mod is a Promise, so calling mod.helper() causes an error.
  3. Final Answer:

    Missing 'await' before import, so mod is a Promise, not the module. -> Option C
  4. 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

  1. 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.
  2. Step 2: Use type annotation and access property

    Typing with typeof import() is correct, and to get apiUrl, use config.default.apiUrl.
  3. Final Answer:

    const config: typeof import('./config') = await import('./config'); console.log(config.default.apiUrl); -> Option A
  4. Quick Check:

    Default export accessed via .default with type = const config: typeof import('./config') = await import('./config'); console.log(config.default.apiUrl); [OK]
Hint: Default exports accessed via .default after dynamic import [OK]
Common Mistakes:
  • Accessing default export directly without .default
  • Not using await with import
  • Ignoring type annotation for imported module
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard