在17号角,我用了

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

但在WebStorm中,我得到了警告

未解析的变量quotationID

在TypeScrip中,属性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)
      );
  }
}

这是res的值:

{
    "code": "PENDING_QUOTATIONS",
    "status": "OK",
    "timestamp": "13.01.2024, 14:33:24",
    "messages": [
        [
            {
                "id": 1,
                "quotationId": 2,
                "receiver": "receiver@example.com",
                "sender": "sender@example.com",
                "date": "2024-01-13T09:27:59.256+00:00"
            }
        ],
        "I am a string"
    ],
    "token": null
}

推荐答案

您可以使用以下命令来代替{{notification.quotationId}}:

{{notification["quotationId"]}}

这是一个快速解决方案,WebStorm也提出了这一建议.

Typescript相关问答推荐

当类型断言函数返回假时,TypScript正在推断从不类型

在这种情况下,如何获得类型安全函数的返回值?

typescript抱怨值可以是未定义的,尽管类型没有定义

类型脚本泛型最初推断然后设置

编剧错误:正在等待Expect(Locator).toBeVisible()

从TypeScrip中的其他类型检索泛型类型

在数据加载后,如何使用NgFor在使用异步管道的可观测对象上生成子组件?

刷新页面时,TypeScrip Redux丢失状态

TypeScrip:从对象中提取和处理特定类型的键

转换器不需要的类型交集

TYPE FOR ARRAY,其中每个泛型元素是单独的键类型对,然后在该数组上循环

对有效枚举值的常量字符串进行常规判断

在抽象类构造函数中获取子类型

在Google授权后由客户端接收令牌

如何在Nextjs路由处理程序中指定响应正文的类型

TypeScript:方法获胜';t接受较窄类型作为参数

如何通过nx找到所有受影响的项目?

如何将 MUI 主题对象的自定义属性与情感样式组件中的自定义props 一起使用?

在 next.js 13.4 中为react-email-editor使用forwardRef和next/dynamic的正确方法

是否可以从 TypeScript 的类型推断中排除this?