I am creating a textarea
element in my TSX markup, and the TS typechecker in Vim complains 2540: Cannot assign to style because it is a read-only property
. Having textarea.style be read-only is a bit weird, given that it can be written to ...
How can I make the error message disappear? Should I cast the input
variable in my code to something else?
I solved it by using:
input.setAttribute('style', 'white-space: pre; position: absolute; left: -9999px;');
Apparently "TypeScript does not have a style property on Element." as was mentioned in a related Stackoverflow answer.
Apparently, I already had the answer ...
Saying the input variable is the specific type made it work.
const input: HTMLTextAreaElement = document.createElement('textarea')
const currentTarget = e.currentTarget as HTMLDivElement
This is probably because createElement
can create all kinds of elements and Typescript has no way of knowing which, as it is based on the input string. So you need to say what it is returning.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With