1

Does it matter whether Number or number is used in Typescript / Javascript? For example does number perform better than Number? I'm also assuming that Javascript will be fine with number / Number and vice versa?

The answer in Typescript primitive types: any difference between the types “number” and “Number” (is TSC case-insensitive)? says never use Number, but does not explain why. In other words it does not say, "If you use Number then this will happen ..." or "If you use Number vs number then typeof or instanceof checks outcomes will be different depending on what type is used".

For example if you use const arr = any[] and then pass a Number instance as an index to the array Javascript throws an exception. I'm not saying this is the case, just providing an example of the type of answer I'm looking for.

Update

Just came across a specific example from my code:

const errorIndex:Array<Number> = [];

Does it matter whether Array<Number> or Array<number> is used?

Ole
  • 41,793
  • 59
  • 191
  • 359
  • I already looked at that answer. Is says never use Number ... but does not really explain why? – Ole Jun 30 '18 at 18:24
  • It's unclear what you mean. An example of the usage you're asking about would help a **lot**. Are you talking about type annotations? Actual use? Both? Something else? – T.J. Crowder Jun 30 '18 at 18:28
  • Well I'm right in the middle of writing this: https://github.com/fireflysemantics/validator and I think I have used number in some places and Number in some others ... so now I'm wondering whether it need to do all Number or number ...So does there exist a single case where it matters whether something is a number or Number instance? – Ole Jun 30 '18 at 18:31
  • Does there exist a single case where it matters mathematically or perhaps for array indexes whether we are using a Number or number instance? – Ole Jun 30 '18 at 18:38
  • 1
    Neither of those, no, because in both cases Number instances will be coerced to their equivalent primative. But Numbers (capital N) are objects. Unless you have a reason for using their object-ness (storing additional properties on them, etc., which would be Very Odd Indeed), there's no reason to use Numbers rather than numbers. The closest thing to a use case I can think of is that you could use `null` to indicate "no number," but there's already the primitive number value `NaN` ("not a number"), so you don't even need that. – T.J. Crowder Jun 30 '18 at 18:44
  • Cool - I just now realized that Typescript supports both number and Number ... and freaked a little ... Hopefully nothing blows up then :) – Ole Jun 30 '18 at 18:48
  • @TJCrowder you still marked it as a duplicate after I explained in great detail why it is not ...??? – Ole Jun 30 '18 at 20:06
  • 1
    @ Ole - **I** voted "unclear." Unfortunately, SO's closure messages are misleading. People have been complaining about it for years, but SE does nothing about it. https://meta.stackexchange.com/questions/269073/dont-say-i-marked-something-as-a-duplicate-when-i-didnt – T.J. Crowder Jul 01 '18 at 08:02
  • Ah - OK - thanks for explaining. I think SO is trying, but they have a little ways to go :) – Ole Jul 01 '18 at 12:39

0 Answers0