admin管理员组

文章数量:1123404

I am using nestJS and prisma ORM for my project. I have task to set proper type to any type. But i am struggling to do it. For example, i have this part of a function code

async findAllByProjectId(
    projectId: string,
    count: boolean,
    userId?: string,
    q?: GetTaskDto,
  ) {
    const timeStamp = Date.now();
    console.log('1', Date.now() - timeStamp);
    let tasks: Task[];
    if (count && projectId) {
      return {
        count: await getCountTasks(this.prisma, projectId, q),
      };
    }
    if (count && !projectId) {
      return {
        count: await getCountTasksByWorkerId(this.prisma, userId, q),
      };
    }
    if (!projectId) {
      tasks = await getTasksByWorkerId(this.prisma, userId, q);
    } else {
      tasks = await getTasks(this.prisma, projectId, q);
    }
    console.log('2', Date.now() - timeStamp);
    let data: TaskList[] = tasks.map((e: any) => {
      // Get workers data
      const workers = e.TaskAssignees.map((taskAssignee: any) => {
        return {
          id: taskAssignee.assignee.id,
          email: taskAssignee.assignee.email,
          name: taskAssignee.assignee.name,
          roleId: taskAssignee.assignee.roleId,
          role: taskAssignee.assignee.Role.name,
        };
      });

There is statement let tasks: Task[];

so in let data: TaskList[] = tasks.map((e: any) the e should be type Task right? since it iterate the tasks. but when i set e to type Task it has so many error. by the way Task is from @prisma/client

Example

Property 'TaskAssignees' does not exist on type '{ id: string; name: string; projectId: string; startDate: Date; endDate: Date; actualStartDate: Date; actualEndDate: Date; calculatedStartDate: Date; duration: Decimal; ... 9 more ...; updatedAt: Date; }'.ts(2339)

or this (but this in next line of the code i show you)

Property 'project' does not exist on type '{ id: string; name: string; projectId: string; startDate: Date; endDate: Date; actualStartDate: Date; actualEndDate: Date; calculatedStartDate: Date; duration: Decimal; ... 9 more ...; updatedAt: Date; }'. Did you mean 'projectId'?ts(2551)

How to handle something like this? It also appear in another code since the getTasks and getTasksByWorkerId get call in so many code.

Appreciate it. Thanks

本文标签: typescriptNestJS and Prisma set any to proper typeStack Overflow