结合 lint-staged 使用 Typescript Compiler API 对 git 改动的文件进行类型检测
.lintstagedrc.json
{
"*.ts?(x)": "tscf"
}
lefthook.yaml
pre-commit:
parallel: true
commands:
tsc:
glob: "*.{ts,tsx}"
run: npx --no-install tscf {staged_files}
npm install ts-check-files -D
tscf [<file> <glob> ...] [--cwd <directory>]
tscf src/a.ts src/b.ts
declare function tscf(args: {
files: string[];
cwd?: string;
}): Promise<[null, string] | [string, null]>;
import tscf from "ts-check-files";
const [error, message] = await tscf({
files,
cwd,
});
if (error) {
console.error(error);
process.exit(1);
}
console.log(message);