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.
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 string
s in subtle and unexpected ways (for example typeof new String('hello')
is "object"
, not "string"
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With