The typeof operator in type context helps you get the type of a variable or value. It makes your code safer by reusing existing types without repeating them.
Typeof operator in type context in Typescript
type NewType = typeof existingVariable;The typeof here is different from the JavaScript typeof operator that returns a string.
This typeof is used only in TypeScript's type annotations.
AgeType that is the same as the type of age, which is number.const age = 30; type AgeType = typeof age;
UserType matching the shape of the user object.const user = { name: 'Anna', active: true }; type UserType = typeof user;
GreetType that is the type of the greet function.function greet() { return 'hello'; } type GreetType = typeof greet;
This program uses typeof in type context to create ScoreType as the type of score. Then it uses this type for the function parameter to ensure only numbers like score are accepted.
const score = 85; type ScoreType = typeof score; function printScore(s: ScoreType) { console.log(`Score is: ${s}`); } printScore(90);
Remember, typeof in type context works only in TypeScript, not in plain JavaScript.
Use typeof in type context with variables, constants, or functions.
typeof in type context gets the type of a variable or value.
It helps keep types consistent and avoid repetition.
Use it to create new types based on existing variables or functions.
Practice
typeof operator do when used in a type context in TypeScript?Solution
Step 1: Understand
In TypeScript,typeofin type contexttypeofused in a type context extracts the type of a variable or value, not its runtime value.Step 2: Differentiate from runtime
Unlike JavaScript's runtimetypeoftypeofthat returns a string, TypeScript'stypeofin type context returns the type itself.Final Answer:
It gets the type of a variable or value. -> Option BQuick Check:
typeofin type context = type extraction [OK]
typeof in types gets the variable's type [OK]- Confusing runtime
typeofwith type contexttypeof - Thinking it returns a string type
- Assuming it creates new variables
user using typeof in TypeScript?Solution
Step 1: Recall correct
The correct syntax istypeofsyntax in type contexttype 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 CQuick Check:
Correct syntax =typeof variablewithout parentheses [OK]
typeof variable without parentheses for types [OK]- Adding parentheses after
typeof - Writing
type ofinstead oftypeof - Using runtime syntax in type context
const age = 30; type AgeType = typeof age; const myAge: AgeType = 25;
What is the type of
myAge?Solution
Step 1: Identify the type of
The variableageageis assigned the number 30, so its type isnumber.Step 2: Understand
This meansAgeType = typeof ageAgeTypeis the type ofage, which isnumber. SomyAgemust be a number.Final Answer:
number -> Option AQuick Check:
typeof age= number [OK]
- Confusing value with type
- Assuming
typeofreturns string - Using wrong type for
myAge
typeof in type context:const name = "Alice"; type NameType = typeof(name); const userName: NameType = "Bob";
Solution
Step 1: Check
In type context,typeofusage in type contexttypeofmust be used without parentheses. Using parentheses causes a syntax error.Step 2: Verify other parts
Variablenameis declared, assigning string is valid, and semicolons are optional in TypeScript.Final Answer:
Parentheses are not allowed aftertypeofin type context. -> Option AQuick Check:
No parentheses aftertypeofin types [OK]
typeof in type declarations [OK]- Adding parentheses after
typeof - Thinking semicolons are mandatory
- Confusing runtime and type syntax
configCopy? const config = {
url: "https://example.com",
timeout: 5000,
debug: true
};
type ConfigType = typeof config;
const configCopy: ConfigType = {
url: "https://test.com",
timeout: 3000,
debug: false
};Choose the best description of
configCopy's type.Solution
Step 1: Analyze the original
Theconfigobjectconfigobject has three properties:url(string),timeout(number), anddebug(boolean).Step 2: Understand
This creates a type matching the shape and types ofConfigType = typeof configconfig.Step 3: Check
configCopyassignmentconfigCopymust match the same property types asconfig, which it does.Final Answer:
An object with propertiesurl(string),timeout(number), anddebug(boolean). -> Option DQuick Check:
typeof config= object with matching properties [OK]
typeof to copy object shape and types exactly [OK]- Assuming
typeofreturns array or function - Ignoring property types in object
- Confusing object with array
Ready to prove it?
Pass the test and this pattern moves unknown
