|
| 1 | +# Types |
| 2 | + |
| 3 | +The **types** subject provides centralized TypeScript type definitions used throughout the docs.github.com codebase. This includes both application-specific types and TypeScript declaration files (`.d.ts`) for third-party libraries that lack native TypeScript support. |
| 4 | + |
| 5 | +## Purpose & Scope |
| 6 | + |
| 7 | +This subject is responsible for: |
| 8 | +- Defining core types for the application (`Context`, `Page`, `ExtendedRequest`, etc.) |
| 9 | +- Providing TypeScript definitions for third-party libraries without official types |
| 10 | +- Maintaining frontmatter schema types that align with our validation logic |
| 11 | +- Exporting shared types for consistent use across all subjects |
| 12 | + |
| 13 | +**Note**: The types defined here are consumed by nearly every subject in the codebase. Changes to core types like `Context` or `Page` can have wide-reaching impacts. |
| 14 | + |
| 15 | +## Architecture & Key Assets |
| 16 | + |
| 17 | +### Key capabilities and their locations |
| 18 | + |
| 19 | +- **`types.ts`**: The primary file containing all application-specific TypeScript types and interfaces. This is manually maintained and includes: |
| 20 | + * `Context` - Request context object extended throughout middleware |
| 21 | + * `Page` - Page object with content, metadata, and rendering methods |
| 22 | + * `ExtendedRequest` - Express Request with custom properties |
| 23 | + * `PageFrontmatter` - Frontmatter schema type aligned with validation |
| 24 | + * `Site`, `Tree`, `SiteTree` - Site structure and navigation types |
| 25 | + * `Version`, `AllVersions` - Version-related types |
| 26 | + * Many more domain-specific types |
| 27 | + |
| 28 | +- **`index.ts`**: Simple re-export module for backward compatibility. Imports should use `@/types` which resolves to this file. |
| 29 | + |
| 30 | +- **`.d.ts` files**: TypeScript declaration files for third-party libraries that don't provide their own types. These allow TypeScript to type-check usage of these libraries throughout the codebase. |
| 31 | + |
| 32 | +## Setup & Usage |
| 33 | + |
| 34 | +### Importing types |
| 35 | + |
| 36 | +Use the absolute import path with the `@/types` alias: |
| 37 | + |
| 38 | +```typescript |
| 39 | +import type { Context, Page, ExtendedRequest } from '@/types' |
| 40 | +``` |
| 41 | + |
| 42 | +### Adding a new application type |
| 43 | + |
| 44 | +1. Add the type definition to `types.ts` |
| 45 | +2. Export it if it should be available to other subjects |
| 46 | +3. Add JSDoc comments to explain complex types |
| 47 | +4. Consider if the type should be co-located with a specific subject instead |
| 48 | + |
| 49 | +### Adding a declaration file for a third-party library |
| 50 | + |
| 51 | +1. Create a new `.d.ts` file named after the package (e.g., `package-name.d.ts`) |
| 52 | +2. Declare the module and its exports with appropriate types |
| 53 | +3. Use `any` sparingly, but it's acceptable when the library structure is truly dynamic |
| 54 | +4. Add comments explaining why types are using `any` if necessary |
| 55 | + |
| 56 | +Example: |
| 57 | +```typescript |
| 58 | +declare module 'some-package' { |
| 59 | + export function someFunction(param: string): void |
| 60 | + export interface SomeType { |
| 61 | + property: string |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +## Data & External Dependencies |
| 67 | + |
| 68 | +### Type sources |
| 69 | +- **Frontmatter schema**: `PageFrontmatter` type is manually maintained to align with the AJV schema in `src/frame/lib/frontmatter.ts` |
| 70 | +- **Third-party libraries**: Declaration files provide types for libraries without native TypeScript support |
| 71 | +- **Domain models**: Types reflect the structure of content files, site tree, version data, etc. |
| 72 | + |
| 73 | +### Dependencies |
| 74 | +- **TypeScript compiler**: All types are processed during the TypeScript compilation step |
| 75 | +- **Subject imports**: Types import from specific subjects (e.g., `@/landings/types`, `@/versions/lib/enterprise-server-releases.d`) |
| 76 | +- **Express types**: `ExtendedRequest` extends Express's `Request` type |
| 77 | + |
| 78 | +### Type consumers |
| 79 | +Nearly every subject in `src/` imports types from this directory. Common consumers include: |
| 80 | +- Middleware (frame, versions, languages, landings, etc.) |
| 81 | +- Rendering logic (content-render, landings) |
| 82 | +- Content linter rules |
| 83 | +- API routes and scripts |
| 84 | + |
| 85 | +## Cross-links & Ownership |
| 86 | + |
| 87 | +### Related subjects |
| 88 | +- **[`src/frame`](../frame/README.md)**: Defines frontmatter validation schema that aligns with `PageFrontmatter` type |
| 89 | +- **[`src/content-render`](../content-render/README.md)**: Uses `Context`, `Page` types extensively for rendering |
| 90 | +- **[`src/content-linter`](../content-linter/README.md)**: Uses declaration files for markdownlint libraries |
| 91 | +- **All subjects**: Nearly every subject imports types from this directory |
| 92 | + |
| 93 | +### Ownership |
| 94 | +- **Team**: Docs Engineering |
| 95 | + |
| 96 | +## Current State & Next Steps |
| 97 | + |
| 98 | +### Known limitations |
| 99 | +- **Manual maintenance**: `PageFrontmatter` type must be manually kept in sync with `src/frame/lib/frontmatter.ts` schema |
| 100 | + * We don't auto-generate from the schema because: (1) it's dynamically constructed with version-specific properties, (2) build tooling complexity, (3) manual control provides better documentation |
| 101 | +- **Wide-reaching changes**: Modifications to core types like `Context` or `Page` affect many subjects |
| 102 | +- **Third-party types**: Declaration files require updates when upgrading the corresponding packages |
| 103 | + |
| 104 | +### Type coverage goals |
| 105 | +- Continue adding declaration files as new third-party libraries are introduced |
| 106 | +- Consider moving subject-specific types to their respective subject directories (e.g., journey types could move to `src/journeys/types.ts`) |
| 107 | +- Improve JSDoc comments on complex types for better IDE experience |
| 108 | + |
| 109 | +### Testing approach |
| 110 | +- Types are validated during `npm run tsc` (TypeScript compilation) |
| 111 | +- No runtime tests exist for types themselves |
| 112 | +- Breaking type changes are caught by TypeScript errors in consuming code |
| 113 | + |
| 114 | +### Contribution guidance |
| 115 | +- **For new types**: Consider whether the type belongs here (shared across subjects) or in a specific subject directory |
| 116 | +- **For type changes**: Search for usage across the codebase (`grep -r "TypeName" src/`) to assess impact |
| 117 | +- **For declaration files**: Match the package name and version you're typing |
| 118 | +- **Style**: Use `type` for simple aliases, `interface` for objects that may be extended |
0 commit comments