i am learning some Typescript and i am putting everthing i learn in a single page I keep encountering this error called
Type 'string[]' is not assignable to type 'string'.ts(2322)
but it still shows the array on the liveserver LiveServer
Will this affect the code or can i ignore?
this is my code
let description: string= `This TypeScript string can
span multiple
lines
`;
let firstName: string = "Testing";
let title: string = "This is a testing page"
let profile: string = `I'm ${firstName}.
${title}`;
console.log(profile);
let testArray: string[];
testArray=['Typescript','Gaming','Javascript','Python'];
console.log(testingArray);
let heading = document.createElement('h1');
heading.textContent = message;
let body = document.createElement('h2');
body.textContent = description;
let Name = document.createElement('h5');
Name.textContent = profile;
let testingArray = document.createElement('h5');
testingArray.textContent = testArray;
document.body.appendChild(heading);
document.body.appendChild(body);
document.body.appendChild(Name);
document.body.appendChild(testingArray); ```
CodePudding user response:
The type of textContent
is a string
and you are storing an array of strings. So
the error is right. You need to convert the array to string using methods like join.
testingArray.textContent = testArray.join();
Or create multiple textContent by looping over the array, anything according to your logic.
document.body.appendChild(heading);
document.body.appendChild(body);
document.body.appendChild(Name);
for(let testEle of testArray) {
let testingArray = document.createElement('h5');
testingArray.textContent = testEle;
document.body.appendChild(testingArray);
}
Will this affect the code or can i ignore?
Well I would say not to ignore, as ignoring didn't help me run it successfully in other live servers like StackBlitz.