我想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)也不起作用。