TypeScript code needs to be compiled into JavaScript before it can be run in a browser or a Node.js environment. The TypeScript compiler (tsc
) reads the .ts
files and generates corresponding .js
files.
Modules in TypeScript are used to organize code into separate units. There are two types of modules: internal (namespaces) and external (ES6 modules). ES6 modules are the recommended way in modern TypeScript projects.
// math.ts
export function add(a: number, b: number): number {
return a + b;
}
// main.ts
import { add } from './math';
const result = add(1, 2);
console.log(result);
TypeScript allows you to define custom types, interfaces, and enums. These type definitions help in making the code more self - documenting and prevent many runtime errors.
interface User {
name: string;
age: number;
}
function greet(user: User) {
return `Hello, ${user.name}!`;
}
To start a TypeScript project, you first need to initialize a package.json
file using npm init -y
. Then, install TypeScript as a development dependency:
npm install typescript --save - dev
Next, create a tsconfig.json
file which contains the compiler options. You can generate a basic tsconfig.json
file using:
npx tsc --init
You can compile your TypeScript code using the tsc
command. If you have a tsconfig.json
file in your project root, simply run:
npx tsc
This will compile all the .ts
files according to the settings in tsconfig.json
.
A common approach is to use a layered architecture. For example, in a web application, you can have the following layers:
src/
├── presentation/
│ ├── components/
│ │ ├── LoginComponent.ts
│ │ └── DashboardComponent.ts
├── business/
│ ├── userService.ts
│ └── orderService.ts
├── data/
│ ├── userRepository.ts
│ └── orderRepository.ts
Another common practice is to group files by feature. For example, if you have a blog application, you can have the following structure:
src/
├── blog/
│ ├── blogList.ts
│ ├── blogDetails.ts
│ ├── blogService.ts
│ └── blogRepository.ts
├── user/
│ ├── userProfile.ts
│ ├── userLogin.ts
│ ├── userService.ts
│ └── userRepository.ts
Each file should have a single responsibility. For example, a service file should only contain the logic related to a specific service, and a component file should only deal with the UI and related event handling.
Adopt a naming convention for files, classes, functions, and variables. For example, use PascalCase for classes and interfaces, camelCase for functions and variables, and kebab - case for file names.
Keep type definitions in separate files or a dedicated types
directory. This makes it easier to manage and reuse types across the project.
src/
├── types/
│ ├── userTypes.ts
│ └── blogTypes.ts
Proper project structure and file organization in TypeScript are essential for building scalable and maintainable applications. By understanding the fundamental concepts, using the right usage methods, following common practices, and adhering to best practices, developers can create a codebase that is easy to understand, modify, and extend. A well - structured project also improves collaboration among team members and reduces the chances of introducing bugs.