What is the difference between types String and string?
TypescriptTypescript Problem Overview
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?
Typescript Solutions
Solution 1 - Typescript
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
Solution 2 - Typescript
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.
Solution 3 - Typescript
For quick readers:
> Don’t ever use the types Number
, String
, Boolean
, Symbol
, or Object
These types refer to non-primitive boxed objects that are almost never used appropriately in JavaScript code. (emphasis mine)
Source: https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html
Solution 4 - Typescript
In JavaScript strings can be either string primitive type or string objects. The following code shows the distinction:
var a: string = 'test'; // string literal
var b: String = new String('another test'); // string wrapper object
console.log(typeof a); // string
console.log(typeof b); // object
Your error:
> Type 'String' is not assignable to type 'string'. 'string' is a > primitive, but 'String' is a wrapper object. > Prefer using 'string' when possible.
Is thrown by the TS compiler because you tried to assign the type string
to a string object type (created via new
keyword). The compiler is telling you that you should use the type string
only for strings primitive types and you can't use this type to describe string object types.
Solution 5 - Typescript
String
vs string
TypeScript: > Argument of type 'String' is not assignable to parameter of type 'string'. > > 'string' is a primitive, but 'String' is a wrapper object. > > Prefer using 'string' when possible.
demo
> String Object
// error
class SVGStorageUtils {
store: object;
constructor(store: object) {
this.store = store;
}
setData(key: String = ``, data: object) {
sessionStorage.setItem(key, JSON.stringify(data));
}
getData(key: String = ``) {
const obj = JSON.parse(sessionStorage.getItem(key));
}
}
> string primitive
// ok
class SVGStorageUtils {
store: object;
constructor(store: object) {
this.store = store;
}
setData(key: string = ``, data: object) {
sessionStorage.setItem(key, JSON.stringify(data));
}
getData(key: string = ``) {
const obj = JSON.parse(sessionStorage.getItem(key));
}
}
Solution 6 - Typescript
Simple answer:
string
=> is a type. e.g. console.log(typeof 'foo') // stringString
=> is an object with some methods to create and manipulate strings.
Solution 7 - Typescript
Based on my personal reference
string
is a type in JavaScript.String
; in JavaScript, all the strings are represented as an instance ofString class
. Read this short article to learn a bit more about the String class
Prefer the string
over the String