paint-brush
How to Use Type Casting in Typescriptby@smpnjn
30,094 reads
30,094 reads

How to Use Type Casting in Typescript

by Johnny Simpson2mJuly 12th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In TypeScript, something will have an 'unknown` type where TypeScript can't discern the specific type something should be. Casting is necessary for some situations, especially when using querySelector. It's a useful way to enforce certain type restrictions on certain types. The way this code is written is unlikely to happen in the real world, but it could occur if you receive an API response of an unknown type, and have to conform it to a type. TypeScript has a number of predefined types for query selectors, but we can't write `let input: HTMLInputElement =...`
featured image - How to Use Type Casting in Typescript
Johnny Simpson HackerNoon profile picture
Johnny Simpson

Johnny Simpson

@smpnjn

Product, Engineering, Web

About @smpnjn
LEARN MORE ABOUT @SMPNJN'S
EXPERTISE AND PLACE ON THE INTERNET.
L O A D I N G
. . . comments & more!

About Author

Johnny Simpson HackerNoon profile picture
Johnny Simpson@smpnjn
Product, Engineering, Web

TOPICS

THIS ARTICLE WAS FEATURED IN...

Permanent on Arweave
Read on Terminal Reader
Read this story in a terminal
 Terminal
Read this story w/o Javascript
Read this story w/o Javascript
 Lite
Also published here
Coffee-web
Trendcdn