Skip to content

Type Assertions

Jorge Castro edited this page Nov 10, 2025 · 1 revision

This won't work because Propery 'value' does not exist on type 'HTMLElement'.

const input1 = document.getElementById('my-input-text');
console.log(input1.value);

Even adding a type annotation won't work

const input1: HTMLInputElement = document.getElementById('my-input-text');
console.log(input1.value);

Nonetheless, applying a type assertion will work

const input1 = document.getElementById('my-input-text') as HTMLInputElement;
console.log(input1.value);

Clone this wiki locally