17

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.

0

1 Answer 1

29

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").

Sign up to request clarification or add additional context in comments.

1 Comment

And object probably does not work with + ;) (I do not know Typescript, just guessing)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.