Paul0515
Paul0515

Reputation: 25465

What is the difference between types String and string?

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?

Upvotes: 560

Views: 255547

Answers (10)

Javier Aviles
Javier Aviles

Reputation: 10815

TL;DR

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

Upvotes: 65

Mizile
Mizile

Reputation: 162

This error message is indicating that you are trying to assign a value of type String to a variable or parameter with type string. In TypeScript, string is a primitive type that represents a sequence of characters, while String is a wrapper object that provides additional methods for working with strings.

The error message is suggesting that you should use the string type instead of String whenever possible, as the string type is more efficient and has fewer associated overhead costs. This is because String is an object, and objects in JavaScript (and therefore TypeScript) are stored on the heap and require additional memory allocation when they are created. In contrast, string values are stored directly on the stack, which makes them more efficient to use.

To fix this error, you will need to ensure that you are using the string type instead of String wherever it is expected. You can do this by simply replacing String with string in your code. For example, if you have a variable with type String, you could change it to have type string like this:

let myString: String = "hello, world!"; // Incorrect
let myString: string = "hello, world!"; // Correct

It is also worth noting that, in general, you should prefer using string over String whenever possible, as it is more efficient and easier to work with.

Upvotes: 2

WadeeSami
WadeeSami

Reputation: 69

JavaScript has 7 primitive types strings, numbers, booleans, null, undefined, symbol, and bigint, The first five have been around since the beginning, The symbol primitive was added in ES2015 and bigint is in the process of being finalized.

Primitives are distinguished from objects by being immutable and not having methods. You might object that strings do have methods

 console.log('primitive'.charAt(3)) // output is "m"

Well, While a string primitive does not have methods, JavaScript also defines a String object type that does. JavaScript freely converts between these types. When you access a method like charAt on a string primitive, JavaScript wraps it in a String object, calls the method, and then throws the object away.

TypeScript models this distinction by having distinct types for the primitives and their object wrappers:

  • string and String
  • number and Number
  • boolean and Boolean
  • symbol and Symbol
  • bigint and BigInt

usually things will work if you use String instead of string, except that in some cases, the compiler will raise an error enter image description here

As you can see, string is assignable to String, but String is not assignable to string.

Follow the advice in the error message and stick with string. All the type declarations that ship with TypeScript use it, as do the typings for almost all other libraries.

Upvotes: 4

Dennis
Dennis

Reputation: 177

  1. string ==> primitive data type.
  2. String ==> non-primitive. it's an object and has access to String class methods and properties

Upvotes: 3

Ogada Stanley Chinedu
Ogada Stanley Chinedu

Reputation: 455

Based on my personal reference

Prefer the string over the String

Upvotes: 2

Masih Jahangiri
Masih Jahangiri

Reputation: 10957

Simple answer:

  • string => is a type. e.g. console.log(typeof 'foo') // string
  • String => is an object with some methods to create and manipulate strings.

Upvotes: 5

xgqfrms
xgqfrms

Reputation: 12206

TypeScript: String vs string

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));
  }
}

enter image description here

Upvotes: 7

Willem van der Veen
Willem van der Veen

Reputation: 36660

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.

Upvotes: 17

Joe Lee-Moyet
Joe Lee-Moyet

Reputation: 1845

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.

Upvotes: 91

Fenton
Fenton

Reputation: 251222

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

Upvotes: 490

Related Questions