Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between types String and string?

Tags:

typescript

Does anyone know the difference between String and string in TypeScript? Am I correct in assuming that they ought to be the same?

var a: String = "test"; var b: string = "another test"; a = b; b = a; // this gives a compiler error! 

Current version of the compiler says:

Type 'String' is not assignable to type 'string'.   'string' is a primitive, but 'String' is a wrapper object.      Prefer using 'string' when possible. 

Is that a bug?

like image 620
Paul0515 Avatar asked Feb 06 '13 10:02

Paul0515


People also ask

What is different between string and string?

In this blog we shall see the difference between string and String. Well technically both of these are the same but with a slight difference. While “string” is a datatype, whereas “String” represents a class. “string” is an alias for System.

What is the difference between string and string class?

Essentially, there is no difference between string and String (capital S) in C#. String (capital S) is a class in the . NET framework in the System namespace. The fully qualified name is System.

What is the difference between string and string in Java?

The main difference between String Literal and String Object is that String Literal is a String created using double quotes while String Object is a String created using the new() operator. String is a set of characters.

What's the difference between string and string in C#?

String stands for System. String and it is a . NET Framework type. string is an alias in the C# language for System.


2 Answers

Here is an example that shows the differences, which will help with the explanation.

var s1 = new String("Avoid newing things where possible"); var s2 = "A string, in TypeScript of type 'string'"; var s3: string; 

String is the JavaScript String type, which you could use to create new strings. Nobody does this as in JavaScript the literals are considered better, so s2 in the example above creates a new string without the use of the new keyword and without explicitly using the String object.

string is the TypeScript string type, which you can use to type variables, parameters and return values.

Additional notes...

Currently (Feb 2013) Both s1 and s2 are valid JavaScript. s3 is valid TypeScript.

Use of String. You probably never need to use it, string literals are universally accepted as being the correct way to initialise a string. In JavaScript, it is also considered better to use object literals and array literals too:

var arr = []; // not var arr = new Array(); var obj = {}; // not var obj = new Object(); 

If you really had a penchant for the string, you could use it in TypeScript in one of two ways...

var str: String = new String("Hello world"); // Uses the JavaScript String object var str: string = String("Hello World"); // Uses the TypeScript string type 
like image 185
Fenton Avatar answered Oct 12 '22 21:10

Fenton


The two types are distinct in JavaScript as well as TypeScript - TypeScript just gives us syntax to annotate and check types as we go along.

String refers to an object instance that has String.prototype in its prototype chain. You can get such an instance in various ways e.g. new String('foo') and Object('foo'). You can test for an instance of the String type with the instanceof operator, e.g. myString instanceof String.

string is one of JavaScript's primitive types, and string values are primarily created with literals e.g. 'foo' and "bar", and as the result type of various functions and operators. You can test for string type using typeof myString === 'string'.

The vast majority of the time, string is the type you should be using - almost all API interfaces that take or return strings will use it. All JS primitive types will be wrapped (boxed) with their corresponding object types when using them as objects, e.g. accessing properties or calling methods. Since String is currently declared as an interface rather than a class in TypeScript's core library, structural typing means that string is considered a subtype of String which is why your first line passes compilation type checks.

like image 32
Joe Lee-Moyet Avatar answered Oct 12 '22 22:10

Joe Lee-Moyet