Modules, Namespaces & Project Structure Flashcards
Import/Export Syntax Internal Modules (Namespaces) vs External Modules Barrel Files Module Resolution Project References (45 cards)
What is the difference between ‘import’ and ‘require’ in TypeScript?
‘import’ is ES6 module syntax preferred in TypeScript; ‘require’ is CommonJS and commonly used in Node.js environments.
What are internal modules (namespaces) in TypeScript?
Namespaces encapsulate code within a global scope using the ‘namespace’ keyword, mainly used in non-module environments.
What are external modules in TypeScript?
External modules are individual files using ES6 module syntax like ‘import’ and ‘export’ to share and isolate code.
What is a barrel file?
A barrel file is an index file that re-exports selected exports from other modules, simplifying imports for consumers.
What is module resolution in TypeScript?
Module resolution is the process TypeScript uses to find and associate module specifiers with actual file paths.
What are project references in TypeScript?
Project references allow a TypeScript project to depend on another project, enabling faster incremental builds and type sharing.
What is an advantage of import/export syntax?
It promotes modular design, supports tree-shaking, and aligns with ES standards for compatibility with modern build tools.
What is a disadvantage of using namespaces?
Namespaces are not well-supported in modern module-based systems and can conflict with bundlers and ES modules.
What are the benefits of barrel files?
They reduce long relative import paths and centralize exports, improving developer experience and maintainability.
What is a disadvantage of barrel files?
They can lead to circular dependencies and obscure the actual module dependency graph.
What is the benefit of using module resolution paths via tsconfig?
It simplifies import paths and avoids fragile deep relative paths.
What is a use case for project references?
Large monorepos with shared libraries or types used across multiple independent TypeScript projects.
How do project references impact performance?
They enable incremental compilation, greatly reducing build times in large projects.
What is a common issue when using namespaces?
They are not compatible with ES module systems and can cause issues with bundlers like Webpack or Rollup.
What is a best practice for module resolution?
Define ‘baseUrl’ and ‘paths’ in tsconfig.json and ensure alignment across tools like Webpack and Jest.
What is a best practice when using barrel files?
Avoid importing the barrel file from modules that it re-exports to prevent circular dependencies.
What is a performance impact of project references?
They speed up builds by avoiding recompilation of unchanged projects, ideal for CI/CD pipelines.
What is a real-world tradeoff of barrel files?
They simplify imports but may increase build and debug complexity due to hidden coupling.
What is an example of using a barrel file?
An ‘index.ts’ file that exports members from ‘service.ts’, ‘model.ts’, and ‘utils.ts’.
What is a common debugging tip for module resolution?
Use ‘tsc –traceResolution’ to trace how modules are being resolved by the compiler.
What is the architectural implication of using project references?
They enforce strong boundaries and modularization in monorepo architectures.
What is a best practice for project references?
Enable ‘composite’ and ‘declaration’ options in all referenced projects’ tsconfig files.
What is the risk of deeply nested relative imports?
They become fragile and harder to refactor; path mapping can help mitigate this.
What is a potential gotcha with module resolution?
Improperly configured ‘paths’ in tsconfig can lead to unresolved modules or runtime errors.