0
0
Typescriptprogramming~20 mins

Typeof operator in type context in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Typeof Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of typeof in type context
What is the output of this TypeScript code when compiled and run in JavaScript?
Typescript
const num = 42;
type NumType = typeof num;
console.log(typeof num);
A"undefined"
B"NumType"
C"typeof num"
D"number"
Attempts:
2 left
💡 Hint
Remember that typeof in JavaScript returns a string describing the runtime type.
Predict Output
intermediate
2:00remaining
Type extraction with typeof operator
Given this TypeScript code, what is the type of variable user2?
Typescript
const user = { name: "Alice", age: 30 };
type UserType = typeof user;
const user2: UserType = { name: "Bob", age: 25 };
AA string
BAn object with properties name (string) and age (number)
CA number
DAn array of strings
Attempts:
2 left
💡 Hint
typeof in type context extracts the type shape of the variable.
🔧 Debug
advanced
2:00remaining
Identify the error with typeof in type context
What error does this TypeScript code produce?
Typescript
const value = 10;
type ValueType = typeof value;
const newValue: ValueType = "10";
ANo error, code runs fine.
BType 'number' is not assignable to type 'string'.
CType 'string' is not assignable to type 'number'.
DSyntaxError: Unexpected token 'typeof'.
Attempts:
2 left
💡 Hint
Check the type of value and what is assigned to newValue.
📝 Syntax
advanced
2:00remaining
Correct use of typeof in type context
Which option correctly uses typeof in a type context to declare a variable with the same type as config?
Typescript
const config = { url: "https://example.com", timeout: 5000 };
A
type ConfigType = typeof config;
const newConfig: ConfigType = { url: "https://test.com", timeout: 3000 };
B
type ConfigType = typeof(config);
const newConfig: ConfigType = { url: "https://test.com", timeout: 3000 };
C
type ConfigType = typeof config();
const newConfig: ConfigType = { url: "https://test.com", timeout: 3000 };
D
type ConfigType = typeof config[];
const newConfig: ConfigType = { url: "https://test.com", timeout: 3000 };
Attempts:
2 left
💡 Hint
typeof in type context does not use parentheses or brackets.
🚀 Application
expert
3:00remaining
Using typeof for dynamic type safety
Consider this TypeScript code. What is the type of settingsCopy after the assignment?
Typescript
const settings = { darkMode: true, version: 1 };
type SettingsType = typeof settings;
function cloneSettings(s: SettingsType): SettingsType {
  return { ...s };
}
const settingsCopy = cloneSettings(settings);
AAn object with properties darkMode (boolean) and version (number)
BAn object with properties darkMode (string) and version (string)
CA boolean value
DA number value
Attempts:
2 left
💡 Hint
typeof extracts the type shape of the original object for use in the function.