Type Definition Files
Global Type Declarations
The application has two primary type definition files:src/mlb.d.ts (1,953 lines)
Comprehensive MLB Stats API type definitions in a global MLB namespace. See API Types for detailed documentation.
src/app.d.ts
SvelteKit-specific type declarations:
TypeScript Configuration
tsconfig.json
The TypeScript configuration extends SvelteKit’s defaults:
strict: true- Maximum type checkingmoduleResolution: "bundler"- Modern module resolution- Path aliases for cleaner imports
Path Aliases
Type-safe import aliases configured in bothtsconfig.json and svelte.config.js:
$lib→src/lib/$ui→src/ui/$pkg→package.json$app/*→ SvelteKit modules (built-in)
Type Patterns
Component Props
Svelte 5 components use TypeScript interfaces for props:- Define interface inline or import from types file
- Use optional properties with default values
- Type callback functions precisely
Generic Functions
The fetch utility uses generics for type-safe API calls:Higher-Order Functions
ThecreatePreset function creates type-safe API presets:
Type Guards
Custom type guards for runtime checks:Utility Types
Leverage TypeScript’s built-in utility types:Discriminated Unions
Type-safe state handling:SvelteKit Types
Page Data Types
Auto-generated types for load functions:Route Parameters
Type-safe parameter matchers:Svelte 5 Runes with TypeScript
$state with Types
$derived with Type Inference
$effect with Async
Type Safety in API Calls
Response Types
All MLB API responses are typed:Parameter Types
Error Handling
Typed Errors
Result Types
Namespace Organization
All MLB types live in theMLB namespace:
- No import needed for types
- Clear categorization
- Autocomplete works everywhere
- Consistent naming
Type Checking
Development
Run type checker during development:CI/CD
Type checking is part of the build process:Best Practices
-
Always type function parameters and return values
-
Use inference when obvious
-
Avoid
any- useunknownif needed -
Type component props explicitly
-
Use const assertions for literal types
-
Leverage type narrowing
-
Use satisfies for object validation
-
Document complex types
Type Safety Benefits
- Catch errors at compile time - Before code runs
- IntelliSense support - Autocomplete in editors
- Refactoring confidence - Rename safely
- Self-documenting code - Types explain intent
- Runtime safety - Fewer null/undefined errors
- Team collaboration - Clear contracts between modules