TypeScript · Junior · Question 29 of 75

How do you narrow string | number to string inside a condition?

Explanation

TypeScript analyzes control flow: after a typeof check, the type inside the branch is narrowed to string, and in else to number. instanceof String checks for a wrapper object, not a primitive. The x is T syntax is used in the signature of type guard functions.

Further reading

Problem with a question

TypeScript · Junior · question 29

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