user16857857
user16857857

Reputation:

Error: Type 'string[]' is not assignable to type 'string'

I have a mini project that I need to make a simple calculator with a history. In a part of this project, it should be deleted if one of the history items is clicked, but I got an error while making this. Do you have a solution to solve this error and a theory? About my solution to delete items you have

Type 'string[]' is not assignable to type 'string'.

let text: string = "";
const historyArrey: Array<String> = [];
let equal = document.getElementsByClassName("btn-blue");

equal[0].addEventListener("click", () => {
    let response = eval(text);
    let historyBox = document.getElementById("history");

    (<HTMLInputElement>document.getElementById('screen')).value = response;

    historyArrey.push(text + "=" + response);
    console.log(historyArrey);

    if (historyBox) {
        historyBox.innerHTML = historyArrey.map((i) => {//error
            return "<p onclick='removeItem()'>" + { i } + "</p>"
        });
        historyArrey.join("<br/>")
    }
})

Upvotes: 0

Views: 89

Answers (1)

Konrad
Konrad

Reputation: 24671

Problem

Your join here:

historyBox.innerHTML = historyArrey.map((i) => {
  return "<p onclick='removeItem()'>" + { i } + "</p>"
});
historyArrey.join("<br/>")

does nothing.

Solution

You have to join the array before assigning

historyBox.innerHTML = historyArrey.map((i) => {
  return "<p onclick='removeItem()'>" + { i } + "</p>"
}).join("<br/>");

Upvotes: 1

Related Questions