TypeScript · Junior · Question 25 of 75

What does the type assertion value as string do?

Explanation

as is a hint to the compiler, not a conversion: if it's actually a number, the error shows up later. Use it when you know more than the compiler (e.g. the element type from querySelector), and prefer narrowing via checks.

Further reading

Problem with a question

TypeScript · Junior · question 25

Test yourself

Take the TypeScript · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz