Angular 键值管道给出错误的返回类型

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

我正在 KeyValuePipe 文档中查看此重载

@param input: Record<K, V> | ReadonlyMap<K, V>
@param compareFn: ((a: KeyValue<K, V>, b: KeyValue<K, V>) => number) | null | undefined
@returns KeyValue<K, V>[]

我的代码:

<mat-select [(ngModel)]="item.teamId">
  <mat-option *ngFor="let option of lookups.teams | keyvalue" [value]="option.key">
    {{ option.value }}
  </mat-option>
</mat-select>

lookups.teams
的类型为
Record<number, string | undefined>
。所以根据文档,我预计
option
KeyValue<number, string | undefined>
。然而事实证明是
KeyValue<string, string | undefined>

我需要

option.key
成为 ngModel 才能工作的数字。我该如何解决它?

angular typescript
1个回答
0
投票

我对过载定义感到困惑。如果输入是Record类型,则输出的key始终是字符串。

来自来源

  /*
   * NOTE: when the `input` value is a simple Record<K, V> object, the keys are extracted with
   * Object.keys(). This means that even if the `input` type is Record<number, V> the keys are
   * compared/returned as `string`s.
   */
© www.soinside.com 2019 - 2024. All rights reserved.