Skip to content

Commit b6d60ce

Browse files
committed
Add TypeScript migration documentation
See #1334 (comment)
1 parent 5ccfb3b commit b6d60ce

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

docs/typescript-migration.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# TypeScript Migration Strategy
2+
3+
This document outlines our current TypeScript migration strategy for the ClearlyDefined service.
4+
We are taking a gradual, incremental approach to improve type safety while maintaining backward compatibility.
5+
6+
## Current State
7+
8+
### What We Have
9+
10+
- **TypeScript Compiler Configuration**: We use TypeScript primarily for type checking JavaScript files via `tsconfig.json`
11+
- **Type Definition Files**: `.d.ts` files provide type information
12+
- **JSDoc Type Annotations**: Many JavaScript files include JSDoc comments for better IDE support and basic type checking
13+
- **TypeScript Checking**: The `tsc` command runs as part of our lint process to catch type errors
14+
15+
### Current TypeScript Configuration
16+
17+
See [`tsconfig.json`][tsconfig] for the current configuration.
18+
19+
**Key aspects of our current setup:**
20+
21+
- **Extends strictest TypeScript configuration**: Uses [`@tsconfig/strictest`][tsconfig-strictest] for maximum type safety, with [`@tsconfig/node18`][tsconfig-node18] for Node.js compatibility
22+
- **JavaScript type checking**: `allowJs: true` and `checkJs: true` enable TypeScript to analyze JavaScript files
23+
- **No compilation**: `noEmit: true` means we only do type checking, no compilation to JavaScript
24+
- **Selective strictness**: `strictNullChecks: false` and `exactOptionalPropertyTypes: false` are disabled as they're difficult to enforce in JavaScript
25+
- **Expanding coverage**: The `include` array covers many core library files and their type definitions
26+
27+
## How to Add New Types
28+
29+
### 1. Create Type Definition Files
30+
31+
For JavaScript modules that need types, create corresponding `.d.ts` files
32+
33+
### 2. Add JSDoc Type Annotations
34+
35+
For existing JavaScript files, add JSDoc comments with type information
36+
37+
### 3. Extend TypeScript Checking
38+
39+
To include new files in TypeScript checking, add them to the `include` array in [`tsconfig.json`][tsconfig]:
40+
41+
```json
42+
{
43+
"include": [
44+
// Existing files
45+
// ...
46+
"your/new/file.js" // Add new files here
47+
]
48+
}
49+
```
50+
51+
## Future Migration Path
52+
53+
### Near-term
54+
55+
1. **Expand Type Coverage**: Add `.d.ts` files for more JavaScript modules
56+
1. **Enhanced JSDoc**: Improve existing JSDoc annotations across the codebase
57+
1. **Incremental TypeScript Checking**: Gradually add more files to the `include` list
58+
59+
### Medium-term
60+
61+
We have several options for deeper TypeScript integration:
62+
63+
#### Option 1: Node.js Native TypeScript Support
64+
65+
Node.js 22.6+ offers native TypeScript support with type stripping:
66+
67+
**Advantages:**
68+
69+
- No build step required
70+
- Lightweight type stripping only
71+
- Compatible with existing tooling
72+
73+
**Requirements:**
74+
75+
- [Node.js 22.6+ with `--experimental-strip-types` flag or Node.js 23.6+][node-typescript], or
76+
- [TypeScript 5.8+ with `erasableSyntaxOnly` enabled][ts-node-native]
77+
78+
**Limitations:**
79+
80+
- Only erasable TypeScript syntax (no enums, namespaces with runtime code, parameter properties)
81+
- Must use explicit `import type` syntax
82+
- File extensions required in imports (e.g., `import './file.ts'`)
83+
84+
#### Option 2: Build-based TypeScript
85+
86+
Traditional TypeScript compilation with a build step:
87+
88+
**Advantages:**
89+
90+
- Full TypeScript feature support
91+
- Better IDE integration
92+
- Stronger type checking
93+
94+
**Requirements:**
95+
96+
- Add build process to deployment pipeline
97+
- Update package.json scripts
98+
- Handle source maps and debugging
99+
100+
## Getting Started
101+
102+
To contribute type definitions:
103+
104+
1. Identify a JavaScript module that would benefit from types
105+
1. Create a corresponding `.d.ts` file with appropriate interfaces and type definitions
106+
1. Use the types in JSDoc comments or import them in JavaScript files
107+
1. Add the file to the TypeScript `include` configuration in [`tsconfig.json`][tsconfig] if it's not already covered
108+
1. Test the types by running `npm run tsc`
109+
1. Update this document as we learn and evolve our approach
110+
111+
[tsconfig]: ../tsconfig.json
112+
[tsconfig-strictest]: https://www.npmjs.com/package/@tsconfig/strictest
113+
[tsconfig-node18]: https://www.npmjs.com/package/@tsconfig/node18
114+
[node-typescript]: https://nodejs.org/api/cli.html#cli_node_experimental_strip_types
115+
[ts-node-native]: https://devblogs.microsoft.com/typescript/announcing-typescript-5-8-beta/#the---erasablesyntaxonly-option

0 commit comments

Comments
 (0)