Javascript 将ValidatorJS isISIN转换为Typescript并获取错误?
将Javascript 将ValidatorJS isISIN转换为Typescript并获取错误?,javascript,typescript,parseint,Javascript,Typescript,Parseint,将isin从validatorjs转换为typescript时,我遇到以下错误: (parameter) character: string No overload matches this call. The last overload gave the following error. Type 'number' is not assignable to type 'string'.ts(2769) lib.es5.d.ts(454, 53): The expected typ
isin
从validatorjs转换为typescript时,我遇到以下错误:
(parameter) character: string
No overload matches this call.
The last overload gave the following error.
Type 'number' is not assignable to type 'string'.ts(2769)
lib.es5.d.ts(454, 53): The expected type comes from the return type of this signature.
lib.es5.d.ts(454, 5): The last overload is declared here.
代码如下:
const checksumStr = target.replace(/[A-Z]/g, character => (parseInt(character, 36)));
VSCOde在(parseInt(字符,36))
下绘制一条红线,并显示错误
想法?是否应该不存在parseInt
以下是整个方法,仅供参考:
/**
* Test whether the target string is an ISBN number.
*
* @param target The string
* @return true if the `target` string is an ISIN number, false otherwise
*/
export function isISIN(target:string) {
assertString(target);
if (!isin.test(target)) {
return false;
}
const checksumStr = target.replace(/[A-Z]/g, character => (parseInt(character, 36)));
let sum = 0;
let digit;
let tmpNum;
let shouldDouble = true;
for (let i = checksumStr.length - 2; i >= 0; i--) {
digit = checksumStr.substring(i, (i + 1));
tmpNum = parseInt(digit, 10);
if (shouldDouble) {
tmpNum *= 2;
if (tmpNum >= 10) {
sum += tmpNum + 1;
} else {
sum += tmpNum;
}
} else {
sum += tmpNum;
}
shouldDouble = !shouldDouble;
}
return parseInt(target.substr(target.length - 1), 10) === (10000 - sum) % 10;
}
这是因为返回一个数字
,并期望收到一个字符串
作为替换
无论如何,代码是有效的,这应该是一个警告
要修复它,请将parseInt结果转换为字符串。比如:
const replacer=character=>{
const intValue=parseInt(字符,36);
返回intValue?intValue.toString():“”
}
const checksumStr=target.replace(/[A-Z]/g,replace);