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

3

9

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

Styracaceous answered 8/1, 2021 at 5:54 Comment(4)
Can you show where pinCode and sliceNumber are defined?Mobster
pincode and sliceNumber both are string.Styracaceous
I am not convinced - can you include the code where they're defined?Mobster
const pinCode = "600028" const sliceNumber = "887277272"Styracaceous
T
15

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)}`
Tia answered 8/1, 2021 at 6:27 Comment(1)
Thanks a lot. You've saved me from hours of frustration. You're the man.Caldarium
I
1

you can convert pinCode and sliceNumber as string

const MemberNumber = MBR${pinCode as string}${sliceNumber as string};

Inexpressive answered 9/10, 2023 at 9:13 Comment(0)
D
0

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)}`
Dropper answered 20/5, 2021 at 2:40 Comment(1)
Sure wish I could understand why TF Angular 15 is making me cast a string to a String!?Schoolmaster

© 2022 - 2024 — McMap. All rights reserved.