Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TS 2540: Cannot assign to style because it is a read-only property

Tags:

typescript

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 ...

vim error How can I make the error message disappear? Should I cast the input variable in my code to something else?

like image 694
oligofren Avatar asked Oct 07 '20 11:10

oligofren


2 Answers

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.

like image 81
gignu Avatar answered Nov 15 '22 05:11

gignu


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.

like image 43
oligofren Avatar answered Nov 15 '22 06:11

oligofren