跳到正文
Skillpedia
网站与界面(前端)75k · #2950

no-explicit-any

它能做什么

这个技能检查 TypeScript 文件中的 “any” 类型,它会关闭类型检查。它面向希望 TypeScript 能及早发现更多错误的开发者。

适用场景

  • ✓你请智能体检查 TypeScript 文件的类型安全问题的时候。
  • ✓代码检查器对 “any” 类型发出警告,而你想正确修复的时候。
  • ✓函数处理来自应用外部的数据,并希望在使用前检查的时候。

技能链接

https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/no-explicit-any/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/no-explicit-any/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请帮我检查 TypeScript 文件,并把 “any” 类型换成更安全的类型吗?”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会找出所有使用 “any” 的地方,包括隐式和断言的情况。它会建议用 “unknown”、泛型或校验来替换,并说明每个值在使用处需要做的检查。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Avoid the any type — use unknown, generics, or type guards instead

The any type is a local opt-out that becomes a global problem: a function returning any propagates unchecked types to every caller, silently undermining the type system across the entire codebase. Replacing any with unknown forces type narrowing at the point of use, turning latent runtime errors into compile-time failures where they are cheapest to fix.

Quick Reference

  • any disables all type checking for a value and cascades to callers
  • unknown is the safe alternative — it requires a type check before use
  • Use generics to express "I accept any type, but it stays consistent"
  • At API boundaries, validate with Zod rather than asserting with as Type

Check

Scan this TypeScript file for uses of the any type (explicit annotations, implicit any from missing types, and type assertions to any). Report each location and suggest a safer alternative.

Fix

Replace the any types in this code with unknown (for values of unknown shape), appropriate generics (for type-consistent operations), or Zod validation (for external data). Show the narrowing or generic constraint needed at each usage site.

Explain

Explain why the any type undermines TypeScript's guarantees, how unknown differs from any, and when a type assertion (as Type) is acceptable versus dangerous.

Code Review

Review all type annotations, function signatures, and external data handling in this file. Flag every explicit or implicit any, any type assertion that lacks a preceding type guard, and any return types inferred as any from untyped dependencies.


For full implementation details, code examples, and framework-specific guidance, see references/rule.md.

Rule page: https://frontendchecklist.io/rules/javascript/no-explicit-any

常见问题

“any” 和 “unknown” 有什么区别?

“any” 会完全关闭检查,而 “unknown” 要求在使用该值前先进行类型检查。

使用类型断言有时可以吗?

这个技能说,断言在某些情况下可以接受,但用来掩盖真正的问题时就很危险。

为什么要在应用的边界验证数据?

这个技能建议使用 Zod 等校验库检查外部数据,而不是对其类型做断言。

详情

作者的原始描述

Use when reviewing TypeScript files for type safety regressions, during code review of functions that handle external data, or when the codebase has ESLint warnings for @typescript-eslint/no-explicit-any.

repo: thedaviddias/Front-End-Checklist

热度

GitHub 星标
75k
排名
#2950
许可证
MIT
更新时间
2026/10/31

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能