Question: I need feedback on which import/export/usage system is better practice in TypeScript. I think #1 is the most common, but which one is generally best-practice in well-made, production codebases?
Context: Two of my previous large-scale codebases were in TypeScript and Go. I’ve used approach #1 for several years before starting to warm up more to the package based import approach. I’ve noticed that TypeScript generally uses direct imports, while Go uses packages. I prefer the package approach (#2a or #2b), but am wondering if the direct import system (#1) is better regarded. Please let me know which one is generally regarded as best practice in scale.
**
Direct Import Example:**
File: /services/aws/api-gateway/createAPIGateway.ts
Function Name: createAPIGateway()
Index.ts:
import createAPIGateway from ‘./createAPIGateway’;
export { createAPIGateway };
Usage:
import { createAPIGateway } from ‘./services/aws/api-gateway/index’;
createAPIGateway();
2a. Package Import Example 1:
File: /services/aws/api-gatewaycreateAPIGateway.ts
Function Name: createAPIGateway()
Index.ts:
import createAPIGateway from ‘./createAPIGateway’;
export const apigateway = { createAPIGateway }
Usage:
import { apigateway } from ‘./services/aws/api-gateway/index’;
apigateway.createAPIGateway();
2b. Package Import Example 2:
File: /services/aws/api-gateway/create.ts
Function Name: create()
Index.ts:
import create from ‘./create’;
export const apigateway = { create }
Usage:
import { apigateway } from ‘./services/aws/api-gateway/index’;
apigateway.create();
This is a question of best practices.
man12930 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.