0
0
Typescriptprogramming~5 mins

Typeof operator in type context in Typescript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AThe value of the variable
BA string describing the type
CThe type of a variable or expression
DAn error
Given const x = 42;, what is the type of typeof x in TypeScript?
Atypeof x
Bnumber
Cstring
Dany
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;
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
What is the main benefit of using typeof in type context?
ARuns faster code
BCreates new variables
CChanges variable values
DAvoids repeating type definitions
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.