Recall & Review
beginner
What does the
typeof operator do in a type context in TypeScript?In a type context,
typeof gets the type of a variable or expression, allowing you to use that type elsewhere in your code.Click to reveal answer
beginner
How is
typeof in a type context different from typeof in JavaScript runtime?typeof in TypeScript type context extracts the type of a variable, while in JavaScript runtime it returns a string describing the type (like 'string' or 'number').Click to reveal answer
beginner
Given <code>const user = { name: 'Alice', age: 30 };</code>, what does <code>type User = typeof user;</code> do?It creates a new type
User that matches the shape of the user object, with properties name as string and age as number.Click to reveal answer
intermediate
Can
typeof be used to get the type of a function in TypeScript?Yes,
typeof can get the type of a function variable, including its parameter types and return type.Click to reveal answer
beginner
Why is using
typeof in type context helpful in TypeScript?It helps keep types consistent and reduces duplication by reusing the type of existing variables or objects automatically.
Click to reveal answer
What does
typeof return in a TypeScript type context?✗ Incorrect
typeof in type context extracts the type, not a string or value.Given
const x = 42;, what is the type of typeof x in TypeScript?✗ Incorrect
typeof x is the type number because x is a number.Which of these is a valid use of
typeof in a type context?✗ Incorrect
You can assign a type alias using
typeof in a type context like type T = typeof someVariable;.Can
typeof be used to get the type of a class instance in TypeScript?✗ Incorrect
typeof can get the type of any variable, including class instances.What is the main benefit of using
typeof in type context?✗ Incorrect
Using
typeof helps avoid repeating type definitions by reusing existing types.Explain how the
typeof operator works in a TypeScript type context and give an example.Think about how you can reuse a variable's type without rewriting it.
You got /3 concepts.
Describe the difference between
typeof in JavaScript runtime and in TypeScript type context.One is about values during running, the other about types during coding.
You got /3 concepts.