0
0
Typescriptprogramming~20 mins

Re-exporting modules in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Re-exporting Modules
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 re-export example?

Consider these two TypeScript files:

/* file: mathUtils.ts */
export function add(a: number, b: number) { return a + b; }
export function sub(a: number, b: number) { return a - b; }
/* file: index.ts */
export { add } from './mathUtils';

What will be the result of this code?

import { add, sub } from './index';
console.log(add(5, 3));
console.log(typeof sub);
Typescript
/* mathUtils.ts */
export function add(a: number, b: number) { return a + b; }
export function sub(a: number, b: number) { return a - b; }

/* index.ts */
export { add } from './mathUtils';

/* main.ts */
import { add, sub } from './index';
console.log(add(5, 3));
console.log(typeof sub);
A8\nfunction
BError: 'sub' is not exported from './index'
C8\nundefined
D8\nnumber
Attempts:
2 left
💡 Hint

Check what is actually exported from index.ts.

Predict Output
intermediate
2:00remaining
What does this wildcard re-export produce?

Given these files:

/* file: helpers.ts */
export const greet = () => 'Hello';
export const bye = () => 'Goodbye';
/* file: index.ts */
export * from './helpers';

What will this code print?

import { greet, bye } from './index';
console.log(greet());
console.log(bye());
Typescript
/* helpers.ts */
export const greet = () => 'Hello';
export const bye = () => 'Goodbye';

/* index.ts */
export * from './helpers';

/* main.ts */
import { greet, bye } from './index';
console.log(greet());
console.log(bye());
AHello\nGoodbye
BError: Cannot find module './helpers'
Cundefined\nGoodbye
DHello\nundefined
Attempts:
2 left
💡 Hint

Remember what export * from does.

🔧 Debug
advanced
2:00remaining
Why does this re-export cause a runtime error?

Look at these files:

/* file: data.ts */
const secret = 42;
export default secret;
/* file: index.ts */
export { secret } from './data';

What error will occur when importing secret from index.ts?

import { secret } from './index';
console.log(secret);
Typescript
/* data.ts */
const secret = 42;
export default secret;

/* index.ts */
export { secret } from './data';

/* main.ts */
import { secret } from './index';
console.log(secret);
ASyntaxError: Unexpected token 'export'
Bundefined
CRuntimeError: Cannot find export 'secret' in './data'
D42
Attempts:
2 left
💡 Hint

Check how default exports are re-exported.

📝 Syntax
advanced
2:00remaining
Which option correctly re-exports a default export?

Given data.ts with a default export:

export default function greet() { return 'Hi'; }

Which of these index.ts files correctly re-exports the default export as a named export greet?

Typescript
/* data.ts */
export default function greet() { return 'Hi'; }

/* index.ts */
// Choose the correct re-export syntax
Aexport default { greet } from './data';
Bexport { greet } from './data';
Cexport * from './data';
Dexport { default as greet } from './data';
Attempts:
2 left
💡 Hint

How do you rename a default export when re-exporting?

🚀 Application
expert
2:00remaining
How many exports does this module have after re-exporting?

Consider these files:

/* file: a.ts */
export const a1 = 1;
export const a2 = 2;
/* file: b.ts */
export const b1 = 'b';
export const b2 = 'bb';
/* file: index.ts */
export * from './a';
export { b1 } from './b';

How many named exports does index.ts have?

Typescript
/* a.ts */
export const a1 = 1;
export const a2 = 2;

/* b.ts */
export const b1 = 'b';
export const b2 = 'bb';

/* index.ts */
export * from './a';
export { b1 } from './b';
A3
B4
C2
D5
Attempts:
2 left
💡 Hint

Count all named exports re-exported by index.ts.