0
0
Typescriptprogramming~20 mins

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

Choose your learning style9 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.