Skip to content

类型安全:消除代码中的 any 类型 #47

@minorcell

Description

@minorcell

问题描述

代码里大量使用 any 类型,类型安全完全失控:

  1. Zustand store 用 any(类型安全:Zustand store 状态用 any #26
  2. catch 块用 any(类型安全:catch 块里直接用 any #23
  3. mock 数据文件没有类型定义(类型缺失:mock 数据文件没有类型定义 #39
  4. API 请求参数用 any(多个地方)
interface AppState {
  currentUser: any;  // 完全不知道结构
  setCurrentUser: (user: any) => void;
}

} catch (error: any) {  // error 是什么不知道
  return NextResponse.json({ error: error.message });
}

影响范围

  • IDE 提示失效
  • 重构时容易出 bug
  • 新人无法了解数据结构

建议方案

1. 定义基础类型

interface User {
  id: string;
  email: string;
  name: string;
  role: 'admin' | 'teacher' | 'hr';
  avatar?: string;
  department?: string;
}

2. catch 块用 unknown

} catch (error) {
  const message = error instanceof Error ? error.message : 'Unknown error';
  console.error('Error:', error);
  return NextResponse.json({ error: message }, { status: 500 });
}

3. 给所有 mock 数据加类型定义

相关 Issue(建议关闭)


本 Issue 由 Memo Code 生成。

@hjx2289206 @AlkaidSTART

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions