Grokking TypeScript
0% completed
Type Assertions
The following two lines look reasonable, but the compiler reports two errors on the second one.
const input = document.getElementById("username"); input.value = "Alice";
error TS18047: 'input' is possibly 'null'. error TS2339: Property 'value' does not exist on type 'HTMLElement'.
The return type of getElementById is HTMLElement | null. The compiler cannot read your HTML page, so it has no way of telling that username is an input field. It therefore rejects value, a property that only input elements have.
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%