我刚开始使用Typescript,并且我对下面的代码有问题。
foo(values: string[] | number[]) {
const mappedValues = values.map((value) => {
return (typeof value === 'number')
? value
: `'${value}'`;
});
}Typescript引发错误
Cannot invoke an expression whose type lacks a call signature. Type '(<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: number, index: number, array: number[]) => U, thisArg?: any) => U[])' has no compatible call signatures.我试过修它,但没有成功。我怎么才能让它工作呢?
发布于 2019-04-06 06:06:02
typescript不确定映射有哪些类型可用,因为foo有两个调用签名(string[]和int[]),但在本例中它可以解析它
foo(values: Array<string | number>) {
const mappedValues = values.map((value) => {
return (typeof value === 'number')
? value
: `'${value}'`;
});
}https://stackoverflow.com/questions/55543853
复制相似问题