TypeScript · Junior · Question 25 of 75
What does the type assertion value as string do?
- ✓Correct answer: Tells the compiler to treat the value as a string, with no runtime check
- 2Converts the value to a string, like
String(value) - 3Checks at runtime that the value is a string and throws otherwise
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.