DEV Community 👩‍💻👨‍💻

Cover image for The difference between String and string in Typescript
Josias Aurel
Josias Aurel

Posted on • Originally published at

The difference between String and string in Typescript

TypeScript is a good advancement in the javascript ecosystem.
Today we are going to talk about the difference between the String and string types in Typescript.

Type error: Type 'String' is not assignable to type 'string'.
Enter fullscreen mode Exit fullscreen mode

Let's take a look at the above error.

To begin, we know that whether we use String or string, we would want to refer to the same thing. But both of those are a little different.

String is a class in JavaScript to construct strings. It provides a way of creating strings using that constructor. What we get using String is an object.
If you check if it's a string or not, you will get *

let name = new String('Mike');
console.log(typeof name); // object
Enter fullscreen mode Exit fullscreen mode

string on the other hand does not exist in JavaScript. Rather it is used in Typescript to tell it it is an actual string.

Therefore, when you are creating a function, type or interface in Typescript, it is recommended to use lowercase string to tell Typescript you want to get an actual string. If you use String, then you will get an object (which is not what you would want).

Thank you for reading.

Other articles of mine that you might like

Oldest comments (0)

In defense of the modern web

I expect I'll annoy everyone with this post: the anti-JavaScript crusaders, justly aghast at how much of the stuff we slather onto modern websites; the people arguing the web is a broken platform for interactive applications anyway and we should start over;

React users; the old guard with their artisanal JS and hand authored HTML; and Tom MacWright, someone I've admired from afar since I first became aware of his work on Mapbox many years ago. But I guess that's the price of having opinions.