Modules, Namespaces & Project Structure Flashcards

Import/Export Syntax Internal Modules (Namespaces) vs External Modules Barrel Files Module Resolution Project References (45 cards)

1
Q

What is the difference between ‘import’ and ‘require’ in TypeScript?

A

‘import’ is ES6 module syntax preferred in TypeScript; ‘require’ is CommonJS and commonly used in Node.js environments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are internal modules (namespaces) in TypeScript?

A

Namespaces encapsulate code within a global scope using the ‘namespace’ keyword, mainly used in non-module environments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are external modules in TypeScript?

A

External modules are individual files using ES6 module syntax like ‘import’ and ‘export’ to share and isolate code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a barrel file?

A

A barrel file is an index file that re-exports selected exports from other modules, simplifying imports for consumers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is module resolution in TypeScript?

A

Module resolution is the process TypeScript uses to find and associate module specifiers with actual file paths.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are project references in TypeScript?

A

Project references allow a TypeScript project to depend on another project, enabling faster incremental builds and type sharing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is an advantage of import/export syntax?

A

It promotes modular design, supports tree-shaking, and aligns with ES standards for compatibility with modern build tools.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a disadvantage of using namespaces?

A

Namespaces are not well-supported in modern module-based systems and can conflict with bundlers and ES modules.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the benefits of barrel files?

A

They reduce long relative import paths and centralize exports, improving developer experience and maintainability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a disadvantage of barrel files?

A

They can lead to circular dependencies and obscure the actual module dependency graph.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the benefit of using module resolution paths via tsconfig?

A

It simplifies import paths and avoids fragile deep relative paths.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a use case for project references?

A

Large monorepos with shared libraries or types used across multiple independent TypeScript projects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do project references impact performance?

A

They enable incremental compilation, greatly reducing build times in large projects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a common issue when using namespaces?

A

They are not compatible with ES module systems and can cause issues with bundlers like Webpack or Rollup.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a best practice for module resolution?

A

Define ‘baseUrl’ and ‘paths’ in tsconfig.json and ensure alignment across tools like Webpack and Jest.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a best practice when using barrel files?

A

Avoid importing the barrel file from modules that it re-exports to prevent circular dependencies.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is a performance impact of project references?

A

They speed up builds by avoiding recompilation of unchanged projects, ideal for CI/CD pipelines.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is a real-world tradeoff of barrel files?

A

They simplify imports but may increase build and debug complexity due to hidden coupling.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is an example of using a barrel file?

A

An ‘index.ts’ file that exports members from ‘service.ts’, ‘model.ts’, and ‘utils.ts’.

20
Q

What is a common debugging tip for module resolution?

A

Use ‘tsc –traceResolution’ to trace how modules are being resolved by the compiler.

21
Q

What is the architectural implication of using project references?

A

They enforce strong boundaries and modularization in monorepo architectures.

22
Q

What is a best practice for project references?

A

Enable ‘composite’ and ‘declaration’ options in all referenced projects’ tsconfig files.

23
Q

What is the risk of deeply nested relative imports?

A

They become fragile and harder to refactor; path mapping can help mitigate this.

24
Q

What is a potential gotcha with module resolution?

A

Improperly configured ‘paths’ in tsconfig can lead to unresolved modules or runtime errors.

25
What is the advantage of using external modules over internal ones?
External modules follow modern JavaScript standards and integrate better with tooling and ecosystems.
26
What is a common interview question about module systems in TypeScript?
Explain the difference between internal (namespaces) and external (ES modules) in TypeScript and when to use each.
27
What is a disadvantage of using project references?
They require more complex configuration and coordination between multiple tsconfig files.
28
What is a debugging tip for circular dependencies in barrel files?
Temporarily remove the barrel file and import modules individually to detect dependency loops.
29
What’s the difference between classic and node module resolution?
Classic is legacy and uses TypeScript’s custom rules; Node uses Node.js rules and is the default today.
30
What’s a good way to organize modules in a large TypeScript app?
Use a layered structure with barrel files for each layer and project references for shared code.
31
Can barrel files be used in a monorepo?
Yes, but they must be carefully managed to avoid circular dependencies and namespace pollution.
32
What does 'composite: true' enable in tsconfig.json?
It allows a project to be referenced by others and enables incremental builds.
33
What’s a gotcha with barrel files and tree-shaking?
If not carefully managed, tree-shaking might include unnecessary code from the barrel exports.
34
What is a reason to prefer ES6 modules over namespaces?
They are standard-compliant, support static analysis, and work well with bundlers and modern toolchains.
35
What happens if two projects reference each other in project references?
It creates a circular project reference, which will result in a compiler error.
36
What is a monitoring strategy for debugging import issues?
Use consistent logging and compiler tracing tools to trace import paths and resolution behavior.
37
What’s an architectural drawback of deep internal module trees?
They can lead to tight coupling and make the codebase harder to refactor or scale.
38
What’s a real-world example of project references?
An enterprise monorepo where a 'common' library is referenced by both frontend and backend apps.
39
What’s a best practice for naming barrel files?
Use 'index.ts' consistently to avoid confusion and align with community conventions.
40
What is the performance implication of not using path aliases?
Refactoring imports is slower and build tools may take longer to resolve relative paths.
41
Should barrel files be used in every folder?
Not necessarily—only use them where they simplify import statements without introducing coupling.
42
What is the advantage of separating module logic by domain?
Improves maintainability and enforces clean boundaries between different application areas.
43
What is a challenge with debugging circular imports?
They may result in partially loaded modules at runtime, causing undefined errors.
44
What is a good rule for using imports in shared libraries?
Avoid importing from implementation-specific modules—import only from public barrel exports.
45
What is the difference between 'export default' and 'export named'?
'default' allows a single export per module, while 'named' supports multiple explicit exports.