Tokitaka / maps-typescript

Make an application for showing a location(latitude, longitude) of user and company using TypeScript & Parcel
0 stars 0 forks source link

Type null is not assignable to type HTMLElement #2

Open Tokitaka opened 2 months ago

Tokitaka commented 2 months ago

DOM manipulation with Typescript

Tokitaka commented 2 months ago

Expected Behavior

If we try to perform DOM manipulation such as document.getElementById('map) with Typescript,

I hope it is recognized as type HTMLElement by Typescript.

Current Behavior

In error message,

Argument of type 'HTMLElement | null' is not assignable to parameter of type 'HTMLElement'.
  Type 'null' is not assignable to type 'HTMLElement'.

Solutions

To inform Typescript that the returned value is an HTMLElement, explicitly cast the value using


document.getElementById('map') as HTMLElement