
ts-expect-error を付与しながら .js を .ts に一括で書き換える
DRANK
やりたいこと巨大なコードベースに対して、 .js をとりあえず .ts に書き換えてしまいたい。だが、素朴な拡張子の書き換えで型違反が出ると jest やその他ツールが止まりはじめて面倒。なので、エラー行には // @ts-expect-error を自動で付与しながら書き換えたい方法ファイルの拡張子を .js から .ts に書き換えるtypescript service 経由で型を検査し、エラー行を集めるエラーが出た行に // @ts-expect-error を付与するコード書き捨てなので汚いですimport * as ts from "typescript"; import { uniq } from "lodash"; import fs from "fs"; import path from "path"; type ErrorLinesMap = { [k: string]: number[] }; function rewriteToTS(files: string[]) { files.map((f) => { fs.renameSync(f, f.replace(/\.js$/, ".ts")); console.log("rename", f, f.replace(/\.js$/, ".ts")); }); } function compile( tsFileNames: string[], options: ts.CompilerOptions ): ErrorLinesMap { const program = ts.createProgram(tsFileNames, options); const emitResult = program.emit(undefined, () => {}); const allDiagnostics = ts .ge…