Operands of '+' operation must either be both strings or both numbers. Consider using a template literal @typescript-eslint/restrict-plus-operands

Viewed 12932

I just using tslint but this block of code throws me a error.

 const MemberNumber = 'MBR' + pinCode + sliceNumber
Operands of '+' operation must either be both strings or both numbers. Consider using a template literal  @typescript-eslint/restrict-plus-operands

I tired with template method again throwing the tslint error.

const MemberNumber = `MBR${pinCode}${sliceNumber}`
 Invalid type "any" of template literal expression  @typescript-eslint/restrict-template-expres

how to fix this.

Thanks

2 Answers

It looks like pinCode or sliceNumber is of type number, so converting it to a string should work:

const MemberNumber = `MBR${String(pinCode)}${String(sliceNumber)}`

Typescript does not cast numbers to string implicitly, so you should cast to string first and then you can make concatenation with string. One or more of your parameters may be number.

This will fix your issue:

const MemberNumber = `MBR${String(pinCode)}${String(sliceNumber)}`
Related