Bird
Raised Fist0
Typescriptprogramming~10 mins

Omit type in Typescript - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to omit the 'age' property from the Person type.

Typescript
type Person = { name: string; age: number; city: string };
type PersonWithoutAge = Omit<Person, [1]>;
Drag options to blanks, or click blank then click option'
A'age'
B'name'
C'city'
D'gender'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the property name in quotes.
Trying to omit a property that does not exist.
2fill in blank
medium

Complete the code to create a type without 'email' and 'phone' properties.

Typescript
type Contact = { email: string; phone: string; address: string };
type ContactInfo = Omit<Contact, [1]>;
Drag options to blanks, or click blank then click option'
A'email'
B'email' | 'phone'
C'phone'
D'address'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting only one property at a time.
Not using quotes around property names.
3fill in blank
hard

Fix the error in the code to omit the 'id' property from the User type.

Typescript
type User = { id: number; username: string; password: string };
type UserWithoutId = Omit<User, [1]>;
Drag options to blanks, or click blank then click option'
Aid
B['id']
C"id"
D'id'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the property name without quotes.
Using an array instead of a union of string literals.
4fill in blank
hard

Fill both blanks to create a type that omits 'title' and 'year' from Movie.

Typescript
type Movie = { title: string; year: number; director: string };
type MovieSummary = Omit<Movie, [1] [2] [3]>;
Drag options to blanks, or click blank then click option'
A'title'
B|
C'year'
D&
Attempts:
3 left
💡 Hint
Common Mistakes
Using & instead of | to separate keys.
Not quoting the property names.
5fill in blank
hard

Fill all three blanks to omit 'street', 'city', and 'zip' from Address.

Typescript
type Address = { street: string; city: string; zip: string; country: string };
type SimpleAddress = Omit<Address, [1] [2] [3]>;
Drag options to blanks, or click blank then click option'
A'street'
B|
C'city'
D'zip'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to include all properties to omit.
Using wrong separators like & or commas.

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

  1. 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.
  2. Step 2: Compare with other utility types

    Other utility types like Partial or Readonly modify properties differently, but Omit specifically removes keys.
  3. Final Answer:

    Creates a new type by removing specified properties from an existing type. -> Option B
  4. 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

  1. Step 1: Recall Omit syntax

    The correct syntax is Omit, where Keys is a string literal or union of keys to remove.
  2. 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.
  3. Final Answer:

    type PersonWithoutAge = Omit; -> Option D
  4. 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

  1. Step 1: Understand Omit effect on User type

    Omit removes the 'password' property, so the new type has only 'id' and 'username'.
  2. Step 2: Match the resulting type

    The type of userWithoutPassword matches { id: number; username: string; } exactly.
  3. Final Answer:

    { id: number; username: string; } -> Option A
  4. 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

  1. 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.
  2. Step 2: Identify the error

    In the code, price is unquoted, causing a syntax error.
  3. Final Answer:

    Missing quotes around 'price' in Omit type. -> Option C
  4. 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?

type Employee = { id: number; name: string; salary: number; department: string; email: string; };
hard
A. type EmployeeContact = Omit;
B. type EmployeeContact = Omit;
C. type EmployeeContact = Omit;
D. type EmployeeContact = Omit;

Solution

  1. Step 1: Understand Omit with multiple keys

    Omit accepts a union of string literals to remove multiple properties at once.
  2. Step 2: Check correct syntax for multiple keys

    The correct syntax is Omit using the pipe symbol to combine keys.
  3. Final Answer:

    type EmployeeContact = Omit; -> Option A
  4. Quick Check:

    Omit with union keys uses 'key1' | 'key2' [OK]
Hint: Use pipe '|' to omit multiple keys in Omit [OK]
Common Mistakes:
  • Using array syntax for keys
  • Missing quotes around keys
  • Passing multiple arguments instead of union
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard