|
| 1 | +# `@ts-migrating` — Progressively Upgrade `tsconfig.json` |
| 2 | + |
| 3 | +🚀 **TypeScript keeps evolving — and your `tsconfig` should too.** |
| 4 | + |
| 5 | +But upgrading often breaks existing code, and fixing all errors at once is unrealistic. |
| 6 | + |
| 7 | +**`@ts-migrating`** helps your team migrate to a desired `tsconfig` gradually and safely. |
| 8 | + |
| 9 | +> I chose `@ts-migrating` (rather than `@ts-migration`) to better reflect the plugin’s progressive and incremental philosophy. |
| 10 | +
|
| 11 | +## 🤖 How does this work? |
| 12 | + |
| 13 | +`@ts-migrating` is a TypeScript plugin that lets you **enable your target tsconfig during development** (in IDEs or editors that use the [TypeScript Language Service](https://github.com/microsoft/typescript/wiki/Using-the-Language-Service-API)) and in CI — **without affecting `tsc` or your production build**. |
| 14 | + |
| 15 | +The philosophy behind the plugin follows three simple steps: |
| 16 | + |
| 17 | +1. 🛑 **Prevention** |
| 18 | + |
| 19 | + * Errors from your target config are surfaced during development and in CI. |
| 20 | + * This ensures no new violations are introduced into the codebase. |
| 21 | + |
| 22 | +2. 🔧 **Reduction** |
| 23 | + |
| 24 | + * Lines marked with `@ts-migrating` are temporarily suppressed. |
| 25 | + * Developers can progressively fix these lines, reducing violations over time. |
| 26 | + |
| 27 | +3. ✅ **Migration** |
| 28 | + |
| 29 | + * Once all violations are fixed, no `@ts-migrating` directives remain. |
| 30 | + * At this point, you're ready to fully adopt the new tsconfig — and the plugin has served its purpose. |
| 31 | + |
| 32 | +## 📚 Overview |
| 33 | + |
| 34 | +`@ts-migrating` consists of two parts: |
| 35 | + |
| 36 | +1. 🔌 **[TypeScript Language Service Plugin](https://github.com/microsoft/TypeScript/wiki/Writing-a-Language-Service-Plugin)** |
| 37 | + |
| 38 | + * Enables IDEs to show errors from the `tsconfig` you're migrating to. |
| 39 | + * Suppresses errors on lines marked with `@ts-migrating`. |
| 40 | + |
| 41 | +2. 🖥️ **Standalone CLI: `ts-migrating`** |
| 42 | + |
| 43 | + * `ts-migrating check` |
| 44 | + |
| 45 | + * Run `@ts-migrating`-aware type checking using your new `tsconfig`. |
| 46 | + * `ts-migrating annotate` |
| 47 | + |
| 48 | + * Automatically mark all errors caused by your new `tsconfig` with `@ts-migrating`. |
| 49 | + * ⚠️ Run this with a clean git state!!! This script will automatically add the `@ts-migrating` directive above every new TypeScript error introduced by your new `tsconfig`. There are edge cases where this will break the code, please review the changes carefully. It is recommended to run your formatter and linter afterwards. |
| 50 | + |
| 51 | +## 🎪 Examples |
| 52 | + |
| 53 | +* [Migrating to `noUncheckedIndexedAccess`](./examples/no-unchecked-indexed-access-migration/src/index.ts): |
| 54 | + |
| 55 | + | Without `@ts-migrating` | With `@ts-migrating` | |
| 56 | + | ----------------------- | -------------------- | |
| 57 | + |  |  | |
| 58 | + |
| 59 | +* [Migrating to `noUncheckedIndexAccess`](./examples/erasable-syntax-only-migration/src/index.ts) |
| 60 | +* [Migrating to `erasableSyntaxOnly`](./examples/erasable-syntax-only-migration/src/index.ts) |
| 61 | + |
| 62 | +## 📦 Install and Setup |
| 63 | + |
| 64 | +This project does **NOT** require any IDE extensions. It relies purely on TypeScript's own Language Service, so it works on most IDEs and editors that support TypeScript (e.g., VSCode, WebStorm). |
| 65 | + |
| 66 | +To install: |
| 67 | + |
| 68 | +```bash |
| 69 | +cd my-cool-project |
| 70 | +npm install -D ts-migrating |
| 71 | +``` |
| 72 | + |
| 73 | +In your existing `tsconfig.json`, add the plugin: |
| 74 | + |
| 75 | +```jsonc |
| 76 | +{ |
| 77 | + // ... |
| 78 | + "compilerOptions": { |
| 79 | + // ... |
| 80 | + "plugins": [ |
| 81 | + { |
| 82 | + "name": "ts-migrating", |
| 83 | + "compilerOptions": { |
| 84 | + // ... put the compiler options you wish to migrate to, for example: |
| 85 | + "strict": true |
| 86 | + } |
| 87 | + } |
| 88 | + ] |
| 89 | + // ... |
| 90 | + } |
| 91 | + // ... |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +ℹ️ *Note: `plugins` only affect the TypeScript Language Service (used by IDEs). They do **not** impact `tsc` or your build.* |
| 96 | + |
| 97 | +🎉 Your codebase is now ready! |
| 98 | + |
| 99 | +### ✅ Verify the Setup |
| 100 | + |
| 101 | +#### 🧑💻 In your IDE |
| 102 | + |
| 103 | +* Restart the IDE, or just the TS server. |
| 104 | +* Confirm that type errors now reflect the new `compilerOptions`. For example, when migrating to strict mode, verify that strict-specific errors appear. |
| 105 | +* Add `// @ts-migrating` before a line with an error — the error should disappear in the IDE. |
| 106 | + |
| 107 | +#### 🖥 In the terminal |
| 108 | + |
| 109 | +* Run: |
| 110 | + |
| 111 | + ```bash |
| 112 | + npx ts-migrating check |
| 113 | + ``` |
| 114 | + |
| 115 | + You should see errors from the new config, excluding those marked with `@ts-migrating`. |
| 116 | + |
| 117 | +### ✨ Optional Next Steps |
| 118 | + |
| 119 | +* Run `npx ts-migrating annotate` to automatically annotate newly introduced errors with `// @ts-migrating`. |
| 120 | +* Replace your CI type-check step with `npx ts-migrating check` to prevent unreviewed errors from slipping through. |
| 121 | + |
| 122 | +## 📣 Shoutout |
| 123 | + |
| 124 | +This project wouldn't be possible without inspiration from: |
| 125 | + |
| 126 | +* [allegro/typescript-strict-plugin](https://github.com/allegro/typescript-strict-plugin): |
| 127 | + Especially for revealing the undocumented [`updateFromProject` option](https://github.com/allegro/typescript-strict-plugin/blob/master/src/plugin/utils.ts#L28-L32) which helped fix a critical issue with the standalone script. [You can find out more here](./src/plugin/mod.ts#L31)). |
| 128 | + |
| 129 | +## 👤 Author |
| 130 | + |
| 131 | +YCM Jason |
0 commit comments