Why even in strict mode TypeScript is not complaining about this
function test(firstName: string, lastName?: string): string {
return firstName + " " + lastName;
}
test('John');
Nor about this
const str: string = '';
const num: object = {};
const result: string = str + num;
I don't remember cases when I would want to get and print on the screen 'John undefined' or '[object Object]'. Is the whole point of type checking is supposed to be catching such errors? (Flow does it)
The “+” operator with a String acts as a concatenation operator. Whenever you add a String value to a double using the “+” operator, both values are concatenated resulting a String object. In-fact adding a double value to String is the easiest way to convert a double value to Strings.
There are two ways to concatenate strings in Java: By + (String concatenation) operator. By concat() method.
The concat() is an inbuilt function in TypeScript which is used to add two or more strings and returns a new single string. Syntax: string. concat(string2, string3[, ..., stringN]);
As MartinJohns said on a github issue regarding this topic
I think this would belong more to the realm of linters. This is not really a type error, it's just undesired behavior.
no-base-to-string
restrict-plus-operands
restrict-template-expressions
no-implicit-coercion
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