SuperAPI AI 工具集

ban-type-assertions

编程与开发

查看项目

更新于 10-15 21:40

技能介绍

用途:在指定 npm 包中禁止 as 类型断言,强制用编译器可验证的方式替代,避免开发者用“信任我”绕过类型检查。

主要能力:启用 @typescript-eslint/consistent-type-assertions 规则并设为 assertionStyle: 'never';按层级替换断言:外部数据边界用 Zod 的 parse/safeParse,联合类型用 switch、in、instanceof 等控制流收窄,库类型缺口才用带说明的 eslint-disable。还涉及将共享 schema 提升到 @factory/common、修正测试 mock 以匹配 schema、在错误处理内解析边界数据。

使用方式:在 packages/<name>/.eslintrc.js 的 rules 中加入 '@typescript-eslint/consistent-type-assertions': ['error', { assertionStyle: 'never' }];用 cd packages/<name> && npm run lint 2>&1 | grep "consistent-type-assertions" 枚举违规;修复后运行 npm run lint、npm run typecheck、npm run test,仓库根运行 npm run knip。

适用场景:TypeScript 包中需要消除 as 断言、加强运行时校验与类型安全的代码库。

限制:需已有或新建 Zod schema;测试 mock 可能需同步调整;库类型缺口等少数情况仍需 eslint-disable 并注明理由;具体平台与收费信息暂未提供。