-1

我想Array.prototype.includes在我的 ts 文件中使用方法,例如。

const id = this.selectedItem.id;
ids.includes(id);

ids数字数组在哪里。我总是得到一个编译错误: Argument of type 'number' is not assignable to parameter of type 'number & string'

当显式将 id 转换为数字Number(id)时,我也看到了Type number is not assignable to type string。当我明确地将 id 转换为字符串时id.toString(),我可以看到Type string is not assignable to type number.

这是压倒性的。如何includes在 TypeScript 中使用方法?如何在我的示例中使用它?

[编辑]扩展示例:

    interface State extends EntityState<IItem> {}

    this.stateSubscription = this.store.select((state: State) => state.items).subscribe(
        val => {
            const selectedId = this.selectedItem ? this.selectedItem.id : null;
            val.ids.includes(selectedId);
    }));

val.ids.includes(selectedId as any)也不起作用。

4

1 回答 1

0

您已声明idsnumber[] & string[]

这意味着您提供给方法的参数应该是类型number[] & string[]

尝试将其声明为number[] | string[],这是其中之一,而不是两者。

于 2019-05-31T07:48:45.763 回答