The Omit type creates a new type by removing one or more specified keys from an existing type.
Click to reveal answer
beginner
How do you use Omit to remove the property age from a type Person?
You write Omit<Person, 'age'> to create a new type without the age property.
Click to reveal answer
intermediate
Given type Person = { name: string; age: number; city: string; }, what is Omit<Person, 'age' | 'city'>?
It is a type with only the name property, because age and city are removed.
Click to reveal answer
beginner
Can Omit be used to remove multiple properties at once?
Yes, you can remove multiple properties by listing them as a union of keys, like Omit<Type, 'key1' | 'key2'>.
Click to reveal answer
intermediate
What is the difference between Pick and Omit types?
Pick creates a type by selecting specific keys, while Omit creates a type by removing specific keys.
Click to reveal answer
What does Omit<User, 'password'> do?
ACreates a type like User but without the password property
BCreates a type with only the password property
CAdds a password property to User
DRenames the password property
✗ Incorrect
Omit removes the specified property, so password is removed from User.
Which syntax correctly removes multiple keys using Omit?
AOmit<Type, 'key1' | 'key2'>
BOmit<Type, 'key1' & 'key2'>
COmit<Type, ['key1', 'key2']>
DOmit<Type, {key1, key2}>
✗ Incorrect
Use a union of keys with | to remove multiple properties.
If type A = {x: number; y: string;}, what is Omit<A, 'z'>?
AType with only z property
BError because z does not exist
CType with x and y properties
DType with no properties
✗ Incorrect
Omit ignores keys not in the type and returns the original type unchanged.
Which is true about Omit?
AIt copies all properties
BIt adds new properties to a type
CIt changes property types
DIt removes properties from a type
✗ Incorrect
Omit removes specified properties from a type.
What is the result of Omit<{a: number; b: string}, 'a'>?
A{a: number; b: string}
B{b: string}
C{}
DError
✗ Incorrect
Property 'a' is removed, so only 'b' remains.
Explain how the Omit type works and give an example removing a property from a type.
Think about removing keys from an object type.
You got /3 concepts.
Compare Omit and Pick types and describe when you might use each.
One removes keys, the other selects keys.
You got /3 concepts.
Practice
(1/5)
1.
What does the Omit utility type do in TypeScript?
easy
A. Adds new properties to an existing type.
B. Creates a new type by removing specified properties from an existing type.
C. Makes all properties in a type optional.
D. Converts all properties in a type to readonly.
Solution
Step 1: Understand the purpose of Omit
The Omit type is designed to create a new type by excluding certain properties from an existing type.
Step 2: Compare with other utility types
Other utility types like Partial or Readonly modify properties differently, but Omit specifically removes keys.
Final Answer:
Creates a new type by removing specified properties from an existing type. -> Option B
Quick Check:
Omit = Remove properties [OK]
Hint: Omit removes keys from a type to exclude them [OK]
Common Mistakes:
Confusing Omit with Partial or Pick
Thinking Omit adds properties
Assuming Omit makes properties optional
2.
Which of the following is the correct syntax to create a new type PersonWithoutAge by omitting the age property from Person?
type Person = { name: string; age: number; };
easy
A. type PersonWithoutAge = Omit;
B. type PersonWithoutAge = Omit<'age', Person>;
C. type PersonWithoutAge = Omit;
D. type PersonWithoutAge = Omit;
Solution
Step 1: Recall Omit syntax
The correct syntax is Omit, where Keys is a string literal or union of keys to remove.
Step 2: Check each option
type PersonWithoutAge = Omit; uses Omit which is correct. type PersonWithoutAge = Omit<'age', Person>; reverses parameters. type PersonWithoutAge = Omit; misses quotes around 'age'. type PersonWithoutAge = Omit; uses array syntax which is invalid.
Final Answer:
type PersonWithoutAge = Omit; -> Option D
Quick Check:
Omit syntax [OK]
Hint: Omit syntax: Omit with quotes [OK]
Common Mistakes:
Forgetting quotes around property names
Swapping type and keys order
Using array syntax for keys
3.
Given the code below, what is the type of userWithoutPassword?
type User = { id: number; username: string; password: string; };
type UserWithoutPassword = Omit;
const userWithoutPassword: UserWithoutPassword = { id: 1, username: 'alice' };
medium
A. { id: number; username: string; }
B. { username: string; password: string; }
C. { id: number; username: string; password: string; }
D. { id: number; password: string; }
Solution
Step 1: Understand Omit effect on User type
Omit removes the 'password' property, so the new type has only 'id' and 'username'.
Step 2: Match the resulting type
The type of userWithoutPassword matches { id: number; username: string; } exactly.
Final Answer:
{ id: number; username: string; } -> Option A
Quick Check:
Omit removes 'password' property [OK]
Hint: Omit removes keys, so missing 'password' here [OK]
Common Mistakes:
Including omitted property in the result
Confusing omitted keys with optional keys
Assuming omitted keys remain in type
4.
Identify the error in the following code snippet:
type Product = { id: number; name: string; price: number; };
type ProductWithoutPrice = Omit;
medium
A. Omit requires an array of keys, not a single key.
B. Omit cannot be used with number properties.
C. Missing quotes around 'price' in Omit type.
D. ProductWithoutPrice should be an interface, not a type.
Solution
Step 1: Check Omit syntax
The second parameter of Omit must be a string literal or union of string literals representing keys, so it must be quoted.
Step 2: Identify the error
In the code, price is unquoted, causing a syntax error.
Final Answer:
Missing quotes around 'price' in Omit type. -> Option C
Quick Check:
Omit keys must be quoted strings [OK]
Hint: Always quote keys in Omit like 'key' [OK]
Common Mistakes:
Forgetting quotes around keys
Using number or variable instead of string literal
Thinking Omit needs arrays for single keys
5.
Given the following types, how can you create a new type EmployeeContact that includes all properties of Employee except salary and department?