kb_
kb_

Reputation: 1335

Typescript variable is declared but never used for string interpolation

I'm getting [ts] 'imgNum' is declared but never used. for the following code where I'm trying to construct an image src string:

let imgNum: string = [...].toString();
imgSrc = '${imgNum}of5.png';

How do I get it not to raise the error when I'm actually using that value?

Upvotes: 2

Views: 804

Answers (1)

rpadovani
rpadovani

Reputation: 7360

Sorry, but Typescript is right, you are not using the imgNum variable, you are assigning to imgSrc a string.

You want to use ` (backtick) instead of ' if you want to include the variable

let imgNum: string = [...].toString();
imgSrc = `${imgNum}of5.png`;

let imgNum = 2;
let imgSrc = '${imgNum}of5.png';
let imgSrc1 = `${imgNum}of5.png`;

console.log(imgSrc);
console.log(imgSrc1);

Upvotes: 5

Related Questions