异步安全
- 作者仓库星标 0
- 作者仓库 skills-registry
- 领域
- 通用
- 作者声明的智能体
- 未找到明确声明;不据此推断已兼容或已测试
- 静态检查
- 88 / 100 · 启发式扫描,不代表运行安全
- 运行环境证据
- 未执行;当前没有隔离运行测试证据
- 作者 / 版本 / 许可证
- @tomevault-io · 未声明许可证
- 上下文用量评级
- 低消耗
- 接入复杂程度
- 需简单配置
- 是否需要外部接口密钥
- 不需要
- 兼容的系统
- 未声明(默认跨平台)
- 运行环境要求
- 未检出明确要求
- 检测到的文件与系统行为
-
- 只读
- 命令执行
- 网络行为
- 仅限本地
- 安装命令数
- 无(仅作为资料)
需要注意: 未声明工具权限范围;实际权限由运行环境决定。
# TypeScript Async and Safety Patterns
Type safety, async correctness, and error handling for TypeScript.
## When to Activate
- Writing or reviewing TypeScript code
- Working with async operations
- Handling errors at system boundaries
- Ensuring type safety in public APIs
## Type Safety
### Avoid `any`
```typescript
// BAD: Disables type checking entirely
function process(data: any): any { }
// GOOD: Use unknown and narrow
function process(data: unknown): Result {
if (!isValidInput(data)) throw new Error('Invalid input')
return transform(data) // data is narrowed
}
```
### No Non-Null Assertion Without Guard
```typescript
// BAD: Assertion without evidence
const name = user!.name
// GOOD: Runtime check first
if (!user) throw new Error('User not found')
const name = user.name
```
### Explicit Return Types on Public APIs
```typescript
// GOOD: Return type is part of the contract
export function calculateScore(items: Item[]): number { }
export async function fetchUser(id: string): Promise<User | null> { }
// Let TypeScript infer for local/private functions
const double = (n: number) => n * 2
```
## Async Correctness
### Parallelize Independent Work
```typescript
// BAD: Sequential when unnecessary
const users = await fetchUsers()
const projects = await fetchProjects()
const stats = await fetchStats()
// GOOD: Parallel execution
const [users, projects, stats] = await Promise.all([
fetchUsers(),
fetchProjects(),
fetchStats(),
])
```
### Never Use async with forEach
```typescript
// BAD: forEach does not await
items.forEach(async (item) => {
await processItem(item) // Fire-and-forget, errors lost
})
// GOOD: for...of for sequential
for (const item of items) {
await processItem(item)
}
// GOOD: Promise.all for parallel
await Promise.all(items.map(item => processItem(item)))
```
### No Floating Promises
Every async call must be `await`ed, `.catch()`ed, or explicitly voided:
```typescript
// BAD: Unhandled rejection
saveAnalytics(data)
// GOOD
await saveAnalytics(data)
// or
saveAnalytics(data).catch(err => logger.error(err))
// or (intentional fire-and-forget)
void saveAnalytics(data)
```
## Error Handling
### Never Swallow Errors
```typescript
// BAD
try { riskyOperation() } catch (e) { }
// GOOD
try {
riskyOperation()
} catch (error) {
logger.error('Operation failed:', error)
throw error // or handle meaningfully
}
```
### Wrap JSON.parse
```typescript
// BAD: Throws on invalid input
const data = JSON.parse(rawString)
// GOOD
let data: unknown
try {
data = JSON.parse(rawString)
} catch {
throw new Error('Invalid JSON input')
}
```
### Throw Error Objects
```typescript
// BAD
throw 'something went wrong'
throw { message: 'error' }
// GOOD
throw new Error('something went wrong')
```
## Immutability
```typescript
// GOOD: Spread operator
const updated = { ...user, name: 'New Name' }
const appended = [...items, newItem]
// BAD: Direct mutation
user.name = 'New Name'
items.push(newItem)
// GOOD: Functional state updates in React
setCount(prev => prev + 1)
// BAD: Stale reference in async context
setCount(count + 1)
```
---
> Source: [buchananwill/ue-claude-scaffold](https://github.com/buchananwill/ue-claude-scaffold) — distributed by [TomeVault](https://tomevault.io).
<!-- tomevault:4.0:skill_md:2026-05-22 -->
章节与文件索引
按说明文件中的顺序排列。
何时激活
没有浮动的承诺
不变性
文件/命令 -> any · await · .catch() · local/private · buchananwill/ue-claude-scaffold · github.com/buchananwill/ue-claude-scaffold
内容校验值 -> 0a2a2d220a80
当前原文的自动摘译,尚未人工审校。
何时激活
编写或审查 TypeScript 代码 使用异步操作 处理系统边界的错误 确保公共 API 中的类型安全
没有浮动的承诺
每个异步调用都必须被
awaited、.catch()ed 或显式作废:不变性
资料来源:buchananwill/ue-claude-scaffold — 由 TomeVault 分发。