“未解析的变量<variable>”,尽管声明了对象的类型

问题描述 投票:0回答:1

在 Angular 17 中,我使用

@for (notification of notifications; track notification) {
   <i>{{notification.quotationId}}</i> 
}

但在 WebStorm 中我收到警告

未解析的变量quoteId

在 TypeScript 中,属性

notifications
的类型设置为
PendingQuotations
,即:

export type PendingQuotations = {
  quotationId: number,
  sender: string,
  date: string
}[]

为什么我会收到此警告?

这就是我设置属性的方式:

export class HeaderComponent implements OnInit {
  public notifications?: PendingQuotations;

  constructor(private generalService: GeneralService) { }

  ngOnInit(): void {
    this.$getNotifications().subscribe((pendingQuotations: PendingQuotations) => {
      if (pendingQuotations.length > 0) {
        this.notifications = pendingQuotations;
      }
    });
  }

  $getNotifications(): Observable<PendingQuotations> {
    return this.generalService.get<Response>('/quotation/pending-quotations')
      .pipe(
        map((res: Response) => res.messages[0] as unknown as PendingQuotations)
      );
  }
}
angular typescript webstorm
1个回答
0
投票

请将类型更改为so

export type PendingQuotations = {
  quotationId: number,
  sender: string,
  date: string
}

我不确定你可以像你一样定义类型,所以你可以尝试这个吗!

export class HeaderComponent implements OnInit {
  public notifications?: PendingQuotations[];

  constructor(private generalService: GeneralService) { }

  ngOnInit(): void {
    this.$getNotifications().subscribe((pendingQuotations: PendingQuotations[]) => {
      if (pendingQuotations.length > 0) {
        this.notifications = pendingQuotations;
      }
    });
  }

  $getNotifications(): Observable<PendingQuotations[]> {
    return this.generalService.get<Response>('/quotation/pending-quotations')
      .pipe(
        map((res: Response) => res.messages[0] as unknown as PendingQuotations[])
      );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.