Skip to main content

TypeScript Configuration

This document details the TypeScript configuration for the STO Education Platform project.

Configuration Files

tsconfig.json

Main TypeScript configuration file that extends specific configurations:
Purpose: Uses project references to separate application and build tool configurations.

tsconfig.app.json

Configuration for the main application code:
Key Settings:
  • target: ES2020 for modern JavaScript features
  • jsx: react-jsx for React 17+ JSX transform
  • strict: Enables all strict type checking options
  • noEmit: TypeScript only checks types, doesn’t emit JavaScript
  • baseUrl/paths: Path mapping for cleaner imports (@/./src/)

tsconfig.node.json

Configuration for Node.js build tools and scripts:
Purpose: Separate configuration for build tools with different requirements than the main app.

Type Safety Features

Strict Mode Enabled

The configuration enables strict TypeScript checking:
  • noImplicitAny: Prevents implicit any types
  • strictNullChecks: Ensures null/undefined safety
  • strictFunctionTypes: Strict checking of function parameter types
  • noImplicitReturns: All code paths must return a value
  • noFallthroughCasesInSwitch: Prevents fallthrough in switch statements

Path Mapping

Clean import paths using @/ prefix:

Type Definitions

Built-in Types

The project includes comprehensive type definitions in /src/types/:
  • index.ts: Core interfaces and types
  • BlogPost.ts: Blog-related types
  • Session.ts: Session management types
  • User.ts: User and profile types
  • Course.ts: Course and education types
  • Payment.ts: Payment processing types

Third-Party Types

Type definitions for external libraries:

Build Integration

Vite Integration

TypeScript works seamlessly with Vite:

Development Experience

Features:
  • Real-time type checking in IDE
  • Auto-completion and IntelliSense
  • Error highlighting and diagnostics
  • Refactoring support
  • Import/export validation
IDE Support:
  • VS Code with TypeScript extension
  • WebStorm/IntelliJ IDEA
  • Vim/Neovim with LSP

Type Checking in CI/CD

Type checking is integrated into the build process:
Benefits:
  • Catches type errors before deployment
  • Ensures type safety across the codebase
  • Prevents runtime type-related bugs

Best Practices

Type Definitions

  1. Use Interfaces for Objects:
  1. Use Types for Unions:
  1. Generic Types for Reusability:

Import/Export Types

Strict Type Checking

  1. Avoid any Type:
  1. Use Type Guards:

Common Patterns

API Response Types

Component Props Types

Hook Return Types

Troubleshooting

Common Issues

  1. Module Resolution Errors:
    • Ensure baseUrl and paths are correctly configured
    • Check import paths match the path mapping
  2. Type Import Errors:
    • Use import type for type-only imports
    • Ensure types are properly exported
  3. Strict Mode Errors:
    • Add explicit types instead of relying on inference
    • Use type assertions when necessary
    • Handle null/undefined cases explicitly

Performance Optimization

  1. Skip Lib Check: Enabled to speed up compilation
  2. Incremental Compilation: TypeScript caches compilation results
  3. Project References: Separate configurations for different parts