0
0
Typescriptprogramming~20 mins

Omit type in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Omit Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this TypeScript code using Omit?
Consider the following TypeScript code that uses the Omit utility type. What will be the output when the code runs?
Typescript
type Person = {
  name: string;
  age: number;
  location: string;
};

const person: Person = { name: "Alice", age: 30, location: "NY" };

function printPersonInfo(p: Omit<Person, "location">) {
  console.log(p);
}

printPersonInfo(person);
A{"name":"Alice","age":30}
B{"name":"Alice","age":30,"location":"NY"}
CTypeScript compilation error
DRuntime error: property 'location' missing
Attempts:
2 left
💡 Hint
Remember that Omit removes keys from the type but does not remove properties from the object passed at runtime.
Predict Output
intermediate
2:00remaining
What is the type of variable 'user' after using Omit?
Given the following TypeScript code, what is the type of the variable user?
Typescript
interface User {
  id: number;
  username: string;
  password: string;
  email: string;
}

type UserWithoutPassword = Omit<User, 'password'>;

const user: UserWithoutPassword = {
  id: 1,
  username: 'john_doe',
  email: 'john@example.com'
};
ATypeScript error: missing property 'password'
B{ id: number; username: string; password: string; email: string; }
C{ id: number; username: string; email: string; password?: string; }
D{ id: number; username: string; email: string; }
Attempts:
2 left
💡 Hint
Omit removes the specified key from the type.
🔧 Debug
advanced
2:00remaining
Why does this code cause a TypeScript error?
Examine the code below. Why does TypeScript raise an error on the assignment to newUser?
Typescript
interface Profile {
  name: string;
  age: number;
  email: string;
}

type ProfileWithoutEmail = Omit<Profile, 'email'>;

const newUser: ProfileWithoutEmail = {
  name: 'Bob',
  age: 25,
  email: 'bob@example.com'
};
ABecause 'email' is a private property and cannot be assigned.
BBecause 'email' is omitted, it cannot be included in the object assigned to 'newUser'.
CBecause 'email' is optional in the original interface, it can be omitted or included.
DBecause 'email' is required, it must be included even if omitted in the type.
Attempts:
2 left
💡 Hint
Omit removes keys from the type, so they cannot appear in objects of that type.
🧠 Conceptual
advanced
1:30remaining
Which statement about the Omit type is true?
Choose the correct statement about the TypeScript Omit utility type.
AOmit only works with interface types, not with type aliases.
BOmit modifies the original type by deleting keys permanently.
COmit creates a new type by removing specified keys from an existing type.
DOmit removes keys from the object at runtime.
Attempts:
2 left
💡 Hint
Think about whether Omit changes types or objects, and if it works with all type forms.
Predict Output
expert
2:30remaining
What is the output of this complex Omit usage with intersection types?
Analyze the following TypeScript code and determine the output of the console.log statement.
Typescript
type A = { a: number; b: string; c: boolean };
type B = { b: string; d: number };

type C = Omit<A & B, 'b'>;

const obj: C = { a: 1, c: true, d: 5 };

console.log(obj);
A{"a":1,"c":true,"d":5}
BTypeScript error: Property 'd' does not exist on type 'Omit<A & B, "b">'.
CTypeScript error: Cannot omit property 'b' from intersection type.
D{"a":1,"b":"string","c":true,"d":5}
Attempts:
2 left
💡 Hint
Omit removes keys from the intersection type, so 'b' is removed but other keys remain.