关于 auto-type-checking
供 Cursor 使用的技能,可在编辑后手动或通过文件编辑钩子检查 TypeScript 类型,帮助编码 Agent 在构建前发现常见类型错误。
主要功能
- 01使用 `tsc --noEmit` 手动检查
- 02通过 Cursor Hooks 自动检查
- 03常见类型错误处理指南
适用场景
- 01编辑 TypeScript 文件后检查类型
- 02通过 Cursor 钩子自动报告错误
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ spencerpauly/ awesome-cursor-skills --skill auto-type-checking安装后在 Claude Code 中输入/auto-type-checking确认已加载。
详细介绍
自动类型检查
在每次编辑后立即发现类型错误,而不是等到构建时才发现。
手动工作流
编辑 TypeScript 文件后,运行:
npx tsc --noEmit 2>&1 | head -30
如果出现错误,请先修复再继续。
使用 Cursor Hooks 自动检查
将以下内容添加到 .cursor/hooks.json,以便在每次文件编辑后自动运行:
{
"hooks": [
{
"event": "afterFileEdit",
"script": "check-types.sh",
"pattern": "**/*.{ts,tsx}"
}
]
}
创建 .cursor/hooks/check-types.sh:
#!/bin/bash
# Quick type check — only reports errors, doesn't block
npx tsc --noEmit --pretty 2>&1 | head -20
exit 0 # Don't block the agent even if there are errors
chmod +x .cursor/hooks/check-types.sh
检查内容
出现类型错误时:
- 缺少导入:
Cannot find name 'X'→ 添加导入 - 类型不匹配:
Type 'string' is not assignable to type 'number'→ 修正类型或值 - 缺少属性:
Property 'x' does not exist on type 'Y'→ 将属性添加到接口中,或修正访问方式 - 空值安全:
Object is possibly 'null'→ 添加空值检查或可选链 - 泛型约束:
Type 'X' does not satisfy the constraint 'Y'→ 修正泛型参数
提示
- 使用
--noEmit执行类型检查而不生成输出文件 - 对于大型项目,
tsc可能运行缓慢——可考虑使用--incremental,或仅检查已更改的文件 - 如果使用路径别名,请确保正确配置
tsconfig.json中的 paths - 对于 monorepo,请从包根目录而非工作区根目录运行
tsc - 配合
grinding-until-pass,实现完整的“修复直到无错误”循环
评论
支持 Markdown