Sunday 15 July 2012

javascript - How do I get Typescript to recognize a Number as a number? -


I am trying to do a simple math, with the number returned with a request. Answer text and a number text box. Here is the code

  // var stored mi: number = new number (request.responseText); Var storedMiles = new number (request.responseText); Var entered the miles = parseint ((& lt; HTMLTextAreaElement & gt; document.getElementById ("txtMiles"));); Var interval meels = (entered mile - stored mi);   

The error that I get from the TypeScript compiler is:

  The type on the right side of an arithmetic operation should be of 'any', 'number 'Or an enum type   

under the compiler entered mi under var intervalMiles = (entered mills - stored mile) line Squiggly is putting the line. P>

I could not find anything in the documentation about fixing that error. Neither have I found a way to make the conversion itself: What is the need for the TypeScript compiler to type both the the entered mile and the stored miles Treat the form so that I can do math?

That's because number is not equal to number. The following will fix it:

  var storedMiles = parseInt (request.responseText);   

The complete code (also referred to as the Steve as Radix):

  var storedMiles = parseInt (request.responseText, 10); Var entered the miles = parseint ((& lt; HTMLTextAreaElement & gt; document.getElementById ("txtMiles")). Value, 10); Var interval meels = (entered mile - stored mi);  
 

More explanation:

You have been given some code to show a basic difference between numbers and numbers: < Pre> var num = 3; Var number = new number (3); Console.log (number of types); // Number Console Logs (number of types); // object



No comments:

Post a Comment