This document outlines the TypeScript configuration and setup for the SumItUp project.
TypeScript has been added to both the backend (Node.js/Express) and frontend (React Native/Expo) parts of the project to provide type safety, better developer experience, and improved code maintainability.
tsconfig.json: Main TypeScript configuration with strict type checking enabled- Path mapping: Configured for clean imports using
@/prefixes - Output directory: Compiled JavaScript goes to
./dist/
{
"devDependencies": {
"typescript": "latest",
"@types/node": "latest",
"@types/express": "latest",
"@types/bcrypt": "latest",
"@types/cors": "latest",
"@types/jsonwebtoken": "latest",
"@types/multer": "latest",
"@types/morgan": "latest",
"@types/nodemailer": "latest",
"@types/passport": "latest",
"@types/passport-facebook": "latest",
"@types/passport-github2": "latest",
"@types/passport-google-oauth20": "latest",
"ts-node": "latest",
"nodemon": "latest",
"tsconfig-paths": "latest"
}
}{
"scripts": {
"build": "tsc",
"start": "node dist/index.js",
"dev": "nodemon --exec ts-node index.ts",
"start:ts": "ts-node index.ts"
}
}The following key files have been converted to TypeScript:
index.ts- Main application entry pointconfig.ts- Configuration with proper typingdb.ts- Database connection with type safetysrc/models/User.ts- User model with interfacessrc/models/Content.ts- Content model with interfacessrc/controllers/auth/login.ts- Example controller conversionsrc/types/index.ts- Shared type definitions
cd backend
npm run dev # Uses ts-node for developmentcd backend
npm run build # Compile TypeScript to JavaScript
npm start # Run compiled JavaScriptThe frontend already had TypeScript support through Expo. The configuration has been enhanced with:
- Strict type checking enabled
- Path mapping for clean imports
- Additional compiler options for better type safety
{
"extends": "expo/tsconfig.base",
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"paths": {
"@/*": ["src/*"],
"@components/*": ["src/components/*"],
"@screens/*": ["src/screens/*"],
"@utils/*": ["src/utils/*"],
"@types/*": ["src/types/*"],
"@services/*": ["src/services/*"]
}
}
}Created comprehensive type definitions in src/types/index.ts including:
- Content types - Matching backend models
- User types - User-related interfaces
- API response types - Standardized API responses
- Navigation types - React Navigation type safety
- Form types - Form validation and handling
The project supports both JavaScript and TypeScript files during the migration period:
- New files should be written in TypeScript (
.ts/.tsx) - Existing files can be gradually converted
- JavaScript files continue to work alongside TypeScript files
To convert a JavaScript file to TypeScript:
- Rename
.jsto.ts(or.jsxto.tsx) - Add type annotations for function parameters and return types
- Define interfaces for complex objects
- Import types from the shared type definitions
- Fix any type errors reported by TypeScript
Before (JavaScript):
const login = async (req, res) => {
const { email, password } = req.body;
// ... rest of the function
};After (TypeScript):
import { Request, Response } from "express";
interface LoginRequest extends Request {
body: {
email: string;
password: string;
};
}
const login = async (req: LoginRequest, res: Response): Promise<Response> => {
const { email, password } = req.body;
// ... rest of the function
};- Compile-time error detection prevents runtime errors
- IntelliSense support in IDEs for better development experience
- Refactoring safety when changing interfaces or function signatures
- Self-documenting code through type annotations
- Consistent interfaces between frontend and backend
- Better maintainability for large codebases
- Auto-completion for object properties and methods
- Parameter hints when calling functions
- Jump to definition functionality in IDEs
- Use interfaces for object shapes
- Export types from dedicated type files
- Use union types for limited string values (e.g.,
'audio' | 'video')
- Type API responses consistently
- Use proper error types instead of
any - Handle null/undefined cases explicitly
- Use ES6 imports/exports instead of CommonJS
- Leverage path mapping for clean imports
- Group imports logically (external, internal, types)
- Module not found errors: Check path mapping in
tsconfig.json - Type errors: Add proper type annotations or use type assertions carefully
- Build errors: Ensure all dependencies have type definitions
# Check TypeScript compilation without emitting files
npx tsc --noEmit
# Watch mode for development
npx tsc --watch
# Generate declaration files
npx tsc --declaration- Convert remaining controllers to TypeScript
- Add middleware typing for better request/response handling
- Implement API client types for frontend-backend communication
- Add unit test types for better test coverage
- Set up ESLint with TypeScript rules for code quality