Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator '+' cannot be applied to types 'String' and 'String' in TypeScript

I am new to TypeScript trying to play with it. But I face a wired problem. When I try to concatenate two String type using + operator it's give an error that is

Operator '+' cannot be applied to types 'String' and 'String'

My Code snap is

var firstName: String = 'Foo';
var lastName: String = 'Bar';
var name = firstName + lastName;

If I use string instead String or add extra ''it works fine. I checked, within JavaScript we can use + on two String object then why it's show error in TypeScript ? Is it bug or feature ? I definitely missing something. Detail explanation appreciated.

like image 215
Engr. Hasanuzzaman Sumon Avatar asked Jul 08 '15 21:07

Engr. Hasanuzzaman Sumon


1 Answers

String is not the same as string. Always use string unless you really, really know what you're up to.

The upper-case name String refers to the boxed version of a primitive string. These should generally be avoided wherever possible -- they don't behave like normal strings in subtle and unexpected ways (for example typeof new String('hello') is "object", not "string").

like image 134
Ryan Cavanaugh Avatar answered Oct 19 '22 23:10

Ryan Cavanaugh