0
0
Typescriptprogramming~20 mins

Extract type in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Extract 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 Extract type example?
Consider the following TypeScript code using the Extract utility type. What is the type of Result?
Typescript
type A = 'apple' | 'banana' | 'cherry';
type B = 'banana' | 'dragonfruit';
type Result = Extract<A, B>;

// What is the type of Result?
A"banana" | "dragonfruit"
B"apple" | "banana"
C"banana"
D"apple" | "banana" | "cherry"
Attempts:
2 left
💡 Hint
Extract keeps only the types that are in both sets.
Predict Output
intermediate
2:00remaining
What is the output type of Extract with object types?
Given these types, what is the type of Selected after using Extract?
Typescript
type Fruit = { name: string; color: string } | { name: string; taste: string };
type SweetFruit = { name: string; taste: string };
type Selected = Extract<Fruit, SweetFruit>;

// What is Selected?
Anever
B{ name: string }
C{ name: string; color: string } | { name: string; taste: string }
D{ name: string; taste: string }
Attempts:
2 left
💡 Hint
Extract picks types from the first union that are assignable to the second.
Predict Output
advanced
2:00remaining
What is the type of Z when Extract has no overlapping types?
Look at this code snippet. What is the type of Z?
Typescript
type X = string | number;
type Y = boolean;
type Z = Extract<X, Y>;

// What is Z?
AZ is never because no types overlap, no error occurs
BTypeScript error because Extract requires overlapping types
CZ is string | number | boolean
DZ is boolean
Attempts:
2 left
💡 Hint
Extract returns never if no types overlap.
Predict Output
advanced
2:00remaining
What is the type of Result after extracting keys from an interface?
Given this interface and Extract usage, what is the type of Result?
Typescript
interface Person {
  name: string;
  age: number;
  location: string;
}
type Keys = keyof Person;
type Result = Extract<Keys, 'age' | 'location' | 'email'>;

// What is Result?
A"age" | "location"
B"name" | "age" | "location" | "email"
C"email"
D"name"
Attempts:
2 left
💡 Hint
Extract picks keys common to both sets.
🧠 Conceptual
expert
2:00remaining
Which option best describes the Extract utility type behavior?
Select the statement that correctly describes what the Extract utility type does in TypeScript.
AExtract<T, U> creates a new type with all types from U that are assignable to T.
BExtract<T, U> creates a new type with all types from T that are assignable to U.
CExtract<T, U> removes all types from T that are assignable to U.
DExtract<T, U> combines all types from T and U into a union.
Attempts:
2 left
💡 Hint
Think about which types from T remain after Extract.