Typescript primitive types: any difference between the types “number” and “Number” (is TSC case-insensitive)?


Typescript primitive types: any difference between the types “number” and “Number” (is TSC case-insensitive)?



I meant to write a parameter of type number, but I misspelled the type, writing Number instead.


number


Number



On my IDE (JetBrains WebStorm) the type Number is written with the same color that is used for the primitive type number, while if I write a name of a class (known or unknown) it uses a different color, so I guess that somehow it recognizes the misspelled type as a correct/almost-correct/sort-of-correct type.


Number


number



When I compile the code, instead of complaining for example that the compiler couldn't found a class named Number, TSC writes this error message:


Number


Illegal property access



Does that mean that number and Number both co-exists as different types?


number


Number



If this is true, which is the difference between those classes?



If this is not the case, then why it simply didn't write the same error message it displays for unknown classes ("The name 'Number' does not exist in the current scope")



This is the code:


class Test
{
private myArray:string = ["Jack", "Jill", "John", "Joe", "Jeff"];

// THIS WORKS
public getValue(index:number):string
{
return this.myArray[index];
}

// THIS DOESN'T WORK: ILLEGAL PROPERTY ACCESS
public getAnotherValue(index:Number):string
{
return this.myArray[index];
}
}




2 Answers
2



JavaScript has the notion of primitive types (number, string, etc) and object types (Number, String, etc, which are manifest at runtime). TypeScript types number and Number refer to them, respectively. JavaScript will usually coerce an object type to its primitive equivalent, or vice versa:


number


Number


var x = new Number(34);
> undefined
x
> Number {}
x + 1
> 35



The TypeScript type system rules deal with this (spec section 3.7) like this:



For purposes of determining subtype, supertype, and assignment
compatibility relationships, the Number, Boolean, and String primitive
types are treated as object types with the same properties as the
‘Number’, ‘Boolean’, and ‘String’ interfaces respectively.





One might add that they are not exactly cross assignable: typescriptlang.org/Playground/…
– basarat
Mar 19 '13 at 2:08





Also to answer the original poster : Yes TSC (like javascript) is case sensitive :)
– basarat
Mar 19 '13 at 5:20



To augment Ryan's answer with guidance from the TypeScript Do's and Don'ts:



Don’t ever use the types Number, String, Boolean, or Object. These types refer to non-primitive boxed objects that are almost never used appropriately in JavaScript code.


/* WRONG */
function reverse(s: String): String;



Do use the types number, string, and boolean.


/* OK */
function reverse(s: string): string;





But typescript documentation says Object. typescriptlang.org/docs/handbook/basic-types.html
– atilkan
May 9 at 15:16






@atilkan That's fun. I guess they aren't taking their own advice.
– Shaun Luttin
May 9 at 16:24





That documentation was written before lower-case object was a thing
– Ryan Cavanaugh
May 10 at 23:32


object





@RyanCavanaugh Guess we should report this.
– atilkan
May 11 at 12:13





@ShaunLuttin What about array, i can't find any lowercase example.
– atilkan
May 11 at 12:25






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API