typescript-async-safety
- Repo stars 0
- Author repo skills-registry
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
// 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
// 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
// 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
// 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
// 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 awaited, .catch()ed, or explicitly voided:
// 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
// BAD
try { riskyOperation() } catch (e) { }
// GOOD
try {
riskyOperation()
} catch (error) {
logger.error('Operation failed:', error)
throw error // or handle meaningfully
}
Wrap JSON.parse
// 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
// BAD
throw 'something went wrong'
throw { message: 'error' }
// GOOD
throw new Error('something went wrong')
Immutability
// 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)
<!-- tomevault:4.0:skill_md:2026-05-22 -->Source: buchananwill/ue-claude-scaffold — distributed by TomeVault.
- Fluxly category
- Other
- Author-declared agents
- No explicit declaration found; this is not inferred or tested compatibility
- Static check
- 88 / 100 · heuristic scan, not runtime safety proof
- Author / version / license
- @tomevault-io · no license declared
- Fluxly token estimate
- Lean
- Fluxly setup estimate
- Guided setup
- External API key
- No requirement detected
- Detected OS requirements
- Unspecified
- Runtime requirements
- Unspecified
- Detected file/system behavior
-
- Read-only
- Shell exec
- Detected network behavior
- Local-only
- Install commands
- None (reference only)
Profile is derived at build time from SKILL.md and install vectors. Subject to drift from author intent.
Heads up: 未限定 allowed-tools,默认拥有全部工具权限。
The current SKILL.md does not define a fixed output example. When to Activate
Writing or reviewing TypeScript code Working with async operations Handling errors at system boundaries
Type Safety
Type Safety
Avoid any
Avoid any
No Non-Null Assertion Without Guard
No Non-Null Assertion Without Guard
Explicit Return Types on Public APIs
Explicit Return Types on Public APIs
Async Correctness
Async Correctness
# 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
… Evidence boundary and execution chain
Author text anchors workflow facts; Fluxly only indexes current sections, terms, files, and commands.
sections -> When to Activate → Type Safety → Avoid any → No Non-Null Assertion Without Guard → Explicit Return Types on Public APIs → Async Correctness
terms -> Type safety, async correctness, and error handling for TypeScript.
files/cmd -> any · await · .catch() · local/private · buchananwill/ue-claude-scaffold · github.com/buchananwill/ue-claude-scaffold
body sha256 -> 0a2a2d220a80
Decide Fit First
Design Intent
How To Use It
Boundaries And Review