How to Use Type Casting in Typescriptby@smpnjn
4,079 reads

How to Use Type Casting in Typescript

July 12th 2022
2 min
by @smpnjn 4,079 reads
tldt arrow
Read on Terminal Reader

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

@smpnjn

Johnny Simpson

About @smpnjn
LEARN MORE ABOUT @SMPNJN'S EXPERTISE AND PLACE ON THE INTERNET.
react to story with heart

RELATED STORIES

L O A D I N G
. . . comments & more!
Hackernoon hq - po box 2206, edwards, colorado 81632, usa