TypeScript TypeScript Functions & Modules 2 — Questions and Answers
Question 1: Which of the following correctly creates a named export in a TypeScript ES module?
- module.exports = { myFunc };
- export { myFunc }; (Correct answer)
- exports.myFunc = myFunc;
- export default myFunc;
Correct answer: export { myFunc };
Named exports use the `export` keyword with curly braces wrapping the identifier, or are placed directly before the declaration.
Question 2: How many `export default` statements can a single TypeScript module have?
- Unlimited — one per exported value
- Up to two — one for a type and one for a value
- Exactly one — a module may have only one default export (Correct answer)
- Zero — default exports are not allowed in TypeScript
Correct answer: Exactly one — a module may have only one default export
A TypeScript/ES module can have at most one `export default`, while it may have any number of named exports.
Question 3: What does `import type { Foo } from './foo'` do differently than `import { Foo } from './foo'`?
- It imports the value and the type together for runtime use
- It imports only the type information, which is fully erased in the compiled JavaScript (Correct answer)
- It imports Foo as a module namespace object
- It makes the import lazy and returns a Promise
Correct answer: It imports only the type information, which is fully erased in the compiled JavaScript
`import type` guarantees the import is type-only and is completely removed during compilation, ensuring it never appears in the emitted JavaScript.
Question 4: What is a 'barrel file' in TypeScript module organization?
- A file that compresses all module imports into a single bundle
- An `index.ts` file that re-exports from multiple sibling modules as a single entry point (Correct answer)
- A file containing only ambient type declarations
- A configuration file that maps module paths to file locations
Correct answer: An `index.ts` file that re-exports from multiple sibling modules as a single entry point
A barrel file (typically `index.ts`) consolidates re-exports from several modules in a directory, letting consumers import from one path instead of many.
Question 5: Which `tsconfig.json` compiler option controls how TypeScript locates module files?
- moduleResolution (Correct answer)
- moduleType
- resolveModules
- importResolution
Correct answer: moduleResolution
The `moduleResolution` option determines the module resolution strategy TypeScript uses (e.g., `node`, `bundler`, `node16`, `nodenext`).
Question 6: What does `export * from './module'` accomplish in TypeScript?
- Re-exports only the default export of the module under a new name
- Re-exports all named exports from the module, excluding the default export (Correct answer)
- Imports the entire module namespace and merges it with the current module's namespace
- Creates type aliases for every export in the specified module
Correct answer: Re-exports all named exports from the module, excluding the default export
`export *` re-exports all named exports from the specified module but does NOT forward the default export; that requires `export { default } from './module'`.
Question 7: What is the primary purpose of `declare module` in a TypeScript `.d.ts` file?
- To create a runtime module wrapper around existing code
- To provide type information for external JavaScript modules that lack their own type definitions (Correct answer)
- To declare a private module namespace scoped to a single file
- To configure how CommonJS modules are imported
Correct answer: To provide type information for external JavaScript modules that lack their own type definitions
`declare module` creates an ambient module declaration, telling the TypeScript compiler about the types of external JavaScript libraries that don't ship with `.d.ts` files.
Which of the following correctly creates a named export in a TypeScript ES module?