Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 类型为'的参数;编号';不可分配给类型为';数目及;字符串';_Arrays_Typescript_Ngrx_Typescript Typings_Ngrx Entity - Fatal编程技术网

Arrays 类型为'的参数;编号';不可分配给类型为';数目及;字符串';

Arrays 类型为'的参数;编号';不可分配给类型为';数目及;字符串';,arrays,typescript,ngrx,typescript-typings,ngrx-entity,Arrays,Typescript,Ngrx,Typescript Typings,Ngrx Entity,我想在我的ts文件中使用Array.prototype.includes方法 const id = this.selectedItem.id; ids.includes(id); 其中,ids是一个数字数组。 我总是遇到编译错误: 类型为“number”的参数不能分配给类型为“number&string”的参数。 当显式地将id转换为数字数字(id)时,我还看到类型number不能分配给类型string。 当我显式地将id转换为字符串id.toString()时,我可以看到类型字符串不能分配给

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

const id = this.selectedItem.id;
ids.includes(id);
其中,
ids
是一个数字数组。 我总是遇到编译错误:
类型为“number”的参数不能分配给类型为“number&string”的参数。

当显式地将id转换为数字
数字(id)
时,我还看到
类型number不能分配给类型string
。 当我显式地将id转换为字符串
id.toString()
时,我可以看到
类型字符串不能分配给类型number

真是太棒了。如何在TypeScript中使用
includes
方法?如何在我的示例中使用它

[编辑]扩展示例:

    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);
    }));
接口状态扩展了EntityState{}
this.stateSubscription=this.store.select((state:state)=>state.items)。订阅(
val=>{
const selectedId=this.selectedItem?this.selectedItem.id:null;
val.ids.包括(选择EDID);
}));

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

您已将
ids
声明为
number[]和string[]

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


尝试将其声明为
number[]| string[]
,它是其中一个,而不是两个

也许你有权利,但是这个接口是一个预定义的东西。我尝试了
const-id:string[]| number[]=val.ids
但是没有结果。尝试了
ids.includes(selectedId as any)
但是:
无法调用类型缺少调用签名的表达式。类型“((searchElement:string,fromIndex?:number)=>boolean)((searchElement:number,fromIndex?:number)=>boolean)”没有兼容的调用签名。
@krzyhub能否请您提供您的问题的详细信息?@krzyhub我说的不是扩展示例,而是关于您所使用的:没有您工作的上下文(库、效果等),我真的帮不了你更多的忙。我认为这个案例不值得。我将尝试其他东西,而不是
Array.prototype.includes
,因为TS太差劲了。谢谢你的努力。