I have read string primitive type in Typescript has no methods. It is just a value. If we need methods (like toLowerCase() for example), we need to work with String type.
Here is what i've tried:
let s = "Hello";
console.log(typeof(s)); // string
s = s.toLowerCase();
I don't understand: I have created a string variable and i was able to call toLowerCase method on it... Can you explain me why this is possible ?
And what's the value-add of String ?
Thanks
stringtype does indeed have all the methods ofStringin TypeScript. In JavaScript, a primitivestringdoesn't "have" methods, but it will become auto-wrapped in aStringobject if you treat it like it does. That means, pragmatically,strings in JavaScript also have methods. SoStringis the wrapper interface forstringprimitives, andstringis essentially a subtype ofString. (more)Stringinterface is what gets updated when strings get new methods in TS, so it needs to exist. There is almost no "value-add" forStringwhen it comes to users; just usestring. Does that fully address the question? If so I'll write up an answer or find a dupe target; if not, what am I missing?