Challenge - 5 Problems
Omit Type Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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);Attempts:
2 left
💡 Hint
Remember that Omit removes keys from the type but does not remove properties from the object passed at runtime.
✗ Incorrect
The Omit type removes the 'location' property from the type expected by the function, so the function expects an object without 'location'. Passing 'person' which has 'location' causes a TypeScript error. However, if the code compiles (e.g., with type assertion), the logged object would include 'location'.
❓ Predict Output
intermediate2: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'
};Attempts:
2 left
💡 Hint
Omit removes the specified key from the type.
✗ Incorrect
Omit creates a type without the 'password' property, so 'user' has only id, username, and email.
🔧 Debug
advanced2: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'
};Attempts:
2 left
💡 Hint
Omit removes keys from the type, so they cannot appear in objects of that type.
✗ Incorrect
The type ProfileWithoutEmail excludes 'email', so including 'email' in the object causes a TypeScript error.
🧠 Conceptual
advanced1:30remaining
Which statement about the Omit type is true?
Choose the correct statement about the TypeScript
Omit utility type.Attempts:
2 left
💡 Hint
Think about whether Omit changes types or objects, and if it works with all type forms.
✗ Incorrect
Omit creates a new type excluding specified keys but does not modify original types or objects at runtime.
❓ Predict Output
expert2: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);Attempts:
2 left
💡 Hint
Omit removes keys from the intersection type, so 'b' is removed but other keys remain.