brainstorming
brainstorming
You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).
typescript-best-practicesUse when writing or reviewing TypeScript for type safety, advanced types, API boundaries, strict-mode migrations, and common anti-pattern avoidance.
Independent GitHub snapshot · Automated classification · Original documentation governs
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