Modular TypeScript: Exporting and Importing Modules
TypeScript's module system is crucial for organizing code into reusable components. This challenge focuses on correctly implementing and utilizing different export types in TypeScript to create a well-structured and maintainable package. You'll be building a simple utility library with various export strategies to understand their nuances.
Problem Description
You are tasked with creating a TypeScript package named my-utils that provides several utility functions. The package should be structured into multiple files, each containing specific functions. You need to implement different export types (named exports, default exports, and re-exports) to expose these functions from the my-utils package. The goal is to demonstrate a clear understanding of how to export and import modules using various TypeScript export mechanisms.
Key Requirements:
src/math.ts: This file should contain two functions:add(named export) andsquare(default export).src/string.ts: This file should contain a functioncapitalize(named export).src/index.ts: This file should act as the main entry point for themy-utilspackage. It should:- Re-export
addandcapitalizefrom their respective files. - Export
squareas the default export of themy-utilspackage.
- Re-export
package.json: Thepackage.jsonfile should be configured to use TypeScript and specify the main entry point asdist/index.js.tsconfig.json: Thetsconfig.jsonfile should be configured to compile the TypeScript files in thesrcdirectory into thedistdirectory. It should also enable module resolution for ES modules.
Expected Behavior:
When importing from my-utils, you should be able to:
- Import
squareusing a default import. - Import
addandcapitalizeusing named imports.
Edge Cases to Consider:
- Ensure that the compiled JavaScript files are correctly structured and reflect the intended exports.
- Handle potential errors during compilation due to incorrect export syntax.
- Verify that the default export is accessible as expected.
Examples
Example 1:
// In a separate file, e.g., app.ts
import square, { add, capitalize } from 'my-utils';
console.log(square(5)); // Output: 25
console.log(add(2, 3)); // Output: 5
console.log(capitalize("hello")); // Output: Hello
Example 2:
// src/math.ts
export function add(a: number, b: number): number {
return a + b;
}
export default function square(x: number): number {
return x * x;
}
// src/string.ts
export function capitalize(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1);
}
// src/index.ts
export * from './math';
export * from './string';
Constraints
- The solution must be written in TypeScript.
- The package should be structured as described above (math.ts, string.ts, index.ts, package.json, tsconfig.json).
- The compiled JavaScript files should be located in a
distdirectory. - The solution should compile without errors.
- The imports and exports should function as described in the expected behavior.
Notes
- Consider using ES module syntax (
export,import) for modern JavaScript compatibility. - Pay close attention to the syntax for named exports, default exports, and re-exports.
- The
package.jsonandtsconfig.jsonfiles are essential for the project to build correctly. Ensure they are properly configured. - You can use a tool like
tsc(TypeScript compiler) to compile your code. You may need to install it globally (npm install -g typescript). - Think about how the different export types affect the way you import and use the functions in other modules.