简体   繁体   English

'number' 类型的参数不能使用 ParseInt 分配给'string' 类型的参数

[英]Argument of type 'number' is not assignable to parameter of type 'string' using ParseInt

I have a function that calculates a lighter/darker color by hex.我有一个 function 用十六进制计算更亮/更暗的颜色。

On my localhost it runs just fine.. but when I try to build in Angular I get the error:在我的本地主机上它运行得很好..但是当我尝试在 Angular 中构建时,我得到了错误:

ERROR in src/app/requests.service.ts:1611:17 - error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
1611  R = parseInt(R * (100 + percent) / 100);

src/app/requests.service.ts:1612:17 - error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
1612  G = parseInt(G * (100 + percent) / 100);

src/app/requests.service.ts:1613:17 - error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
1613  B = parseInt(B * (100 + percent) / 100);

Here is my code:这是我的代码:

  adjust(color, percent) {
  
   var R = parseInt(color.substring(1,3),16);
   var G = parseInt(color.substring(3,5),16);
   var B = parseInt(color.substring(5,7),16);

   R = parseInt(R * (100 + percent) / 100);
   G = parseInt(G * (100 + percent) / 100);
   B = parseInt(B * (100 + percent) / 100);

   R = (R<255)?R:255;
   G = (G<255)?G:255;
   B = (B<255)?B:255;

   var RR = ((R.toString(16).length==1)?"0"+R.toString(16):R.toString(16));
   var GG = ((G.toString(16).length==1)?"0"+G.toString(16):G.toString(16));
   var BB = ((B.toString(16).length==1)?"0"+B.toString(16):B.toString(16));

   return "#"+RR+GG+BB;
}

I'm calling the function using:我打电话给 function 使用:

 adjust("#345678", -70)

I also tried defining the function parameters like this:我还尝试定义 function 参数,如下所示:

adjust(color:string, percent:number) {

but it wasn't successful.但它并不成功。

Any ideas would be great.任何想法都会很棒。 Thank you!谢谢!

I check your code.我检查你的代码。 The method of parseInt in typescript use is like this. typescript使用中parseInt的方法是这样的。

parseInt(string)

So I change your function like this.所以我像这样改变你的 function 。 You can see below.你可以在下面看到。

function adjust(color:string, percent:number) {
  
  var R = parseInt(color.substring(1,3),16);
  var G = parseInt(color.substring(3,5),16);
  var B = parseInt(color.substring(5,7),16);

  R = parseInt(R * (100 + percent) / 100 + '');
  G = parseInt(G * (100 + percent) / 100 + '');
  B = parseInt(B * (100 + percent) / 100 + '');

  R = (R<255)?R:255;
  G = (G<255)?G:255;
  B = (B<255)?B:255;

  var RR = ((R.toString(16).length==1)?"0"+R.toString(16):R.toString(16));
  var GG = ((G.toString(16).length==1)?"0"+G.toString(16):G.toString(16));
  var BB = ((B.toString(16).length==1)?"0"+B.toString(16):B.toString(16));

  return "#"+RR+GG+BB;
}

I ended up figuring out where the issue was.. here is the code that worked:我最终弄清楚问题出在哪里..这是有效的代码:

adjust(color, percent) {

   var R = parseInt(color.substring(1,3),16);
   var G = parseInt(color.substring(3,5),16);
   var B = parseInt(color.substring(5,7),16);

   R = Math.floor(R * (100 + percent) / 100);
   G = Math.floor(G * (100 + percent) / 100);
   B = Math.floor(B * (100 + percent) / 100);

   R = (R<255)?R:255;
   G = (G<255)?G:255;
   B = (B<255)?B:255;

   var RR = ((R.toString(16).length==1)?"0"+R.toString(16):R.toString(16));
   var GG = ((G.toString(16).length==1)?"0"+G.toString(16):G.toString(16));
   var BB = ((B.toString(16).length==1)?"0"+B.toString(16):B.toString(16));

   return "#"+RR+GG+BB;
}

I had to change the second row of parseInt s to Math.floor .我不得不将parseInt的第二行更改为Math.floor

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “数字”类型的参数不能分配给“字符串”类型的参数 - Argument of type 'number' is not assignable to parameter of type 'string' “字符串”类型的参数不能分配给“数字”类型的参数 - Argument of type 'string' is not assignable to parameter of type 'number' &#39;string | 类型的参数 number&#39; 不能分配给类型为 &#39;string&#39; 的参数。 “数字”类型不能分配给“字符串”类型 - Argument of type 'string | number' is not assignable to parameter of type 'string'. Type 'number' is not assignable to type 'string' 类型&#39;string |的参数 编号 string []&#39;不可分配给&#39;string&#39;类型的参数 - Argument of type 'string | number | string[]' is not assignable to parameter of type 'string' 参数类型编号不可分配给参数类型字符串 | 未定义类型编号不可分配给类型字符串 - Argument type number is not assignable to parameter type string | undefined Type number is not assignable to type string 如何修复“数字”类型的参数不可分配给“字符串”类型的参数 | 正则表达式'' - How to fix ' Argument of type 'number' is not assignable to parameter of type 'string | RegExp' ' 参数类型Number | Function不能赋予参数类型Number - Argument type Number is not assignable to parameter type String|Function 如何解决“&#39;number&#39; 类型的参数不能分配给&#39;string | RegExp&#39; 类型的参数。” - How to fix "Argument of type 'number' is not assignable to parameter of type 'string | RegExp'." “数字”类型的参数不能分配给“日期”类型的参数 - Argument of type 'number' is not assignable to parameter of type 'Date' “RegExp”类型的参数不能分配给“string”类型的参数 - Argument of type 'RegExp' is not assignable to parameter of type 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM