Loading...
Loading...
Compare original and translation side by side
any// Unknown for external data
function parse(input: unknown): User { ... }
// Union for specific options
type Status = 'loading' | 'success' | 'error';
// Generics for reusable code
function first<T>(arr: T[]): T | undefined { ... }
// Type assertion as last resort (with runtime check)
if (isUser(data)) { return data as User; }{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true
}
}any// 外部数据使用Unknown类型
function parse(input: unknown): User { ... }
// 特定选项使用联合类型
type Status = 'loading' | 'success' | 'error';
// 通用代码使用泛型
function first<T>(arr: T[]): T | undefined { ... }
// 类型断言作为最后手段(需配合运行时检查)
if (isUser(data)) { return data as User; }{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true
}
}src/
features/
auth/
components/
hooks/
api.ts
types.ts
index.ts # Barrel: controlled exports
orders/
...
shared/
ui/
utils/@features/*@shared/*../../../../src/
features/
auth/
components/
hooks/
api.ts
types.ts
index.ts # 桶文件:受控导出
orders/
...
shared/
ui/
utils/@features/*@shared/*../../../../const { data, isLoading, error } = useQuery({
queryKey: ['user', userId],
queryFn: () => fetchUser(userId),
});type AuthState =
| { status: 'idle' }
| { status: 'loading' }
| { status: 'authenticated'; user: User }
| { status: 'error'; error: AppError };const { data, isLoading, error } = useQuery({
queryKey: ['user', userId],
queryFn: () => fetchUser(userId),
});type AuthState =
| { status: 'idle' }
| { status: 'loading' }
| { status: 'authenticated'; user: User }
| { status: 'error'; error: AppError };async function fetchUser(id: string): Promise<User> {
try {
const res = await fetch(`/api/users/${id}`);
if (!res.ok) throw new ApiError(res.status, await res.text());
return res.json();
} catch (error) {
throw new AppError('Failed to fetch user', { cause: error });
}
}AbortControllerfinallyasync function fetchUser(id: string): Promise<User> {
try {
const res = await fetch(`/api/users/${id}`);
if (!res.ok) throw new ApiError(res.status, await res.text());
return res.json();
} catch (error) {
throw new AppError('Failed to fetch user', { cause: error });
}
}AbortControllerfinally| Tool | Purpose |
|---|---|
| pnpm | Package manager (declare in |
| Vitest | Testing (unit/integration/e2e) |
| tsup | Builds (ESM + CJS + .d.ts) |
| ESLint | Linting ( |
| Prettier | Formatting |
| 工具 | 用途 |
|---|---|
| pnpm | 包管理器(需在 |
| Vitest | 测试工具(单元/集成/端到端测试) |
| tsup | 构建工具(生成ESM、CJS与.d.ts文件) |
| ESLint | 代码检查(设置 |
| Prettier | 代码格式化 |
useStateuseEffect/controllers/services/modelseslint-disableuseStateuseEffect/controllers/services/modelseslint-disable