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?
AThe value of the variable
BA string describing the type
CThe type of a variable or expression
DAn error
✗ 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?
Atypeof x
Bnumber
Cstring
Dany
✗ 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?
Atype T = typeof someVariable;
Blet x = typeof someVariable;
Cconsole.log(typeof someVariable);
Dtypeof someVariable = string;
✗ 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?
AYes, it gets the instance type
BNo, it only works with primitives
COnly with functions
DOnly with interfaces
✗ Incorrect
typeof can get the type of any variable, including class instances.
What is the main benefit of using typeof in type context?
ARuns faster code
BCreates new variables
CChanges variable values
DAvoids repeating type definitions
✗ 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.
Practice
(1/5)
1. What does the typeof operator do when used in a type context in TypeScript?
easy
A. It returns the runtime value of a variable.
B. It gets the type of a variable or value.
C. It converts a type to a string.
D. It creates a new variable.
Solution
Step 1: Understand typeof in type context
In TypeScript, typeof used in a type context extracts the type of a variable or value, not its runtime value.
Step 2: Differentiate from runtime typeof
Unlike JavaScript's runtime typeof that returns a string, TypeScript's typeof in type context returns the type itself.
Final Answer:
It gets the type of a variable or value. -> Option B
Quick Check:
typeof in type context = type extraction [OK]
Hint: Remember: typeof in types gets the variable's type [OK]
Common Mistakes:
Confusing runtime typeof with type context typeof
Thinking it returns a string type
Assuming it creates new variables
2. Which of the following is the correct syntax to create a new type based on the type of variable user using typeof in TypeScript?
easy
A. type UserType = typeof(user);
B. type UserType = type of user;
C. type UserType = typeof user;
D. type UserType = type(user);
Solution
Step 1: Recall correct typeof syntax in type context
The correct syntax is type NewType = typeof variableName; without parentheses.
Step 2: Check each option
type UserType = typeof user; uses correct syntax. type UserType = type of user; has spaces and is invalid. type UserType = typeof(user); uses parentheses which is invalid in type context. type UserType = type(user); uses incorrect keyword.
Final Answer:
type UserType = typeof user; -> Option C
Quick Check:
Correct syntax = typeof variable without parentheses [OK]
Hint: Use typeof variable without parentheses for types [OK]
Common Mistakes:
Adding parentheses after typeof
Writing type of instead of typeof
Using runtime syntax in type context
3. Consider the following TypeScript code:
const age = 30;
type AgeType = typeof age;
const myAge: AgeType = 25;
What is the type of myAge?
medium
A. number
B. string
C. boolean
D. any
Solution
Step 1: Identify the type of age
The variable age is assigned the number 30, so its type is number.
Step 2: Understand AgeType = typeof age
This means AgeType is the type of age, which is number. So myAge must be a number.
Final Answer:
number -> Option A
Quick Check:
typeof age = number [OK]
Hint: Check the original variable's type to know the new type [OK]
Common Mistakes:
Confusing value with type
Assuming typeof returns string
Using wrong type for myAge
4. Identify the error in this TypeScript code using typeof in type context:
const name = "Alice";
type NameType = typeof(name);
const userName: NameType = "Bob";
medium
A. Parentheses are not allowed after typeof in type context.
B. Variable name is not declared.
C. Type alias cannot be assigned a string.
D. Missing semicolon after const declaration.
Solution
Step 1: Check typeof usage in type context
In type context, typeof must be used without parentheses. Using parentheses causes a syntax error.
Step 2: Verify other parts
Variable name is declared, assigning string is valid, and semicolons are optional in TypeScript.
Final Answer:
Parentheses are not allowed after typeof in type context. -> Option A
Quick Check:
No parentheses after typeof in types [OK]
Hint: Avoid parentheses after typeof in type declarations [OK]
Common Mistakes:
Adding parentheses after typeof
Thinking semicolons are mandatory
Confusing runtime and type syntax
5. Given the code below, what is the type of configCopy?