openclaw-skills/typescript-best-practices/SKILL.md
Use when writing or reviewing TypeScript for type safety, advanced types, API boundaries, strict-mode migrations, and common anti-pattern avoidance.
npx skillsauth add seaworld008/commonly-used-high-value-skills typescript-best-practicesInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
TypeScript is more than just "JavaScript with types." It is a powerful structural type system that can provide profound safety if used correctly. This skill guides you through professional patterns and advanced type programming.
any 或 as any,亟需纠正。T extends U ? X : Y)。Partial, Readonly, Record)。is 关键字编写自定义判断函数。这是 TS 处理多种状态(如 Loading/Success/Error)的最佳实践。
kind, type, status)。never 类型确保 switch/case 覆盖了所有可能的分支。TypeScript 仅在编译时有效。对于外部数据(API 响应、表单输入),必须在运行时校验。
z.infer<T> 生成静态类型。undefined 和“未设置”。type ApiResponse =
| { status: 'success'; data: string[] }
| { status: 'error'; message: string }
| { status: 'loading' };
function handleResponse(response: ApiResponse) {
switch (response.status) {
case 'success':
console.log(response.data.map(d => d.toUpperCase()));
break;
case 'error':
console.error(response.message);
break;
case 'loading':
showSpinner();
break;
default:
// 这里的 response 是 never 类型,确保所有 case 都已处理
const _exhaustiveCheck: never = response;
return _exhaustiveCheck;
}
}
import { z } from 'zod';
const UserSchema = z.object({
id: z.string().uuid(),
username: z.string().min(3),
email: z.string().email(),
role: z.enum(['admin', 'user']),
});
// 从 Schema 提取静态类型
type User = z.infer<typeof UserSchema>;
async function fetchUser(id: string): Promise<User> {
const response = await fetch(`/api/users/${id}`);
const data = await response.json();
// 关键:在运行时通过 Zod 校验外部数据
return UserSchema.parse(data);
}
{
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"noImplicitAny": true,
"strictNullChecks": true,
"exactOptionalPropertyTypes": true,
"noUnusedLocals": true
}
}
any。Generated by Skill Master - Professional Edition
development
Enumerating failure modes via pre-mortem analysis. Systematically identifies failure scenarios for plans, designs, and features, scoring them with RPN/AP. Does not write code.
testing
Orchestrating specialist AI agent teams as a meta-coordinator. Decomposes requests into minimum viable chains, spawns each as an independent session in AUTORUN modes, and drives to final output. Use when a task spans multiple specialist domains, requires parallel agent execution, or needs hub-and-spoke routing across the skill ecosystem.
development
Converting document formats (Markdown/Word/Excel/PDF/HTML). Converts specs from Scribe and reports from Harvest into distributable formats; generates reusable conversion scripts. Use when converting documents, building accessibility-compliant PDFs, or creating Pandoc/LibreOffice pipelines.
testing
Curating cross-agent knowledge and guarding institutional memory. Extracts patterns from agent journals into METAPATTERNS.md, detects knowledge decay, propagates best practices, prevents organizational forgetting. Use when consolidating cross-agent insights, curating memory, or auditing knowledge decay.