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
JavaScript
x
31
31
1
let description: string= `This TypeScript string can
2
span multiple
3
lines
4
`;
5
6
let firstName: string = "Testing";
7
let title: string = "This is a testing page"
8
let profile: string = `I'm ${firstName}.
9
${title}`;
10
11
console.log(profile);
12
13
let testArray: string[];
14
15
testArray=['Typescript','Gaming','Javascript','Python'];
16
console.log(testingArray);
17
18
let heading = document.createElement('h1');
19
heading.textContent = message;
20
let body = document.createElement('h2');
21
body.textContent = description;
22
let Name = document.createElement('h5');
23
Name.textContent = profile;
24
let testingArray = document.createElement('h5');
25
testingArray.textContent = testArray;
26
document.body.appendChild(heading);
27
document.body.appendChild(body);
28
document.body.appendChild(Name);
29
document.body.appendChild(testingArray); ```
30
31
Advertisement
Answer
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.
JavaScript
1
2
1
testingArray.textContent = testArray.join();
2
Or create multiple textContent by looping over the array, anything according to your logic.
JavaScript
1
9
1
document.body.appendChild(heading);
2
document.body.appendChild(body);
3
document.body.appendChild(Name);
4
for(let testEle of testArray) {
5
let testingArray = document.createElement('h5');
6
testingArray.textContent = testEle;
7
document.body.appendChild(testingArray);
8
}
9
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.