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);/* 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);
Check what is actually exported from index.ts.
The index.ts file only re-exports add. The sub function is not exported from index.ts, so attempting to import it causes an error: Error: 'sub' is not exported from './index'.
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());/* 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());
Remember what export * from does.
The export * from './helpers' re-exports all exports from helpers.ts. So both greet and bye are available from index.ts. The calls print 'Hello' and 'Goodbye'.
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);/* data.ts */ const secret = 42; export default secret; /* index.ts */ export { secret } from './data'; /* main.ts */ import { secret } from './index'; console.log(secret);
Check how default exports are re-exported.
The data.ts file exports secret as a default export. The index.ts tries to re-export a named export secret, which does not exist. This causes a runtime error when importing secret from index.ts.
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?
/* data.ts */ export default function greet() { return 'Hi'; } /* index.ts */ // Choose the correct re-export syntax
How do you rename a default export when re-exporting?
Option D uses the correct syntax to re-export the default export as a named export called greet. Option D tries to re-export a named export greet which does not exist. Option D re-exports all named exports but not the default. Option D is invalid syntax.
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?
/* 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';
Count all named exports re-exported by index.ts.
index.ts re-exports all exports from a.ts (which are a1 and a2) plus only b1 from b.ts. So total named exports are 3.