0
0
Typescriptprogramming~20 mins

Exclude type in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exclude 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 Exclude type example?

Consider the following TypeScript code using Exclude. What is the type of Result?

Typescript
type Letters = 'a' | 'b' | 'c' | 'd';
type Result = Exclude<Letters, 'a' | 'd'>;

// What is the type of Result?
A"b" | "c" | "d"
B"b" | "c"
C"a" | "b" | "c" | "d"
D"a" | "d"
Attempts:
2 left
💡 Hint

The Exclude type removes the types on the right from the union on the left.

Predict Output
intermediate
2:00remaining
What does this Exclude type produce?

Given this code, what is the type of Filtered?

Typescript
type Status = 'success' | 'error' | 'loading';
type Filtered = Exclude<Status, 'loading'>;
A"success" | "error"
B"loading"
C"success" | "error" | "loading"
Dnever
Attempts:
2 left
💡 Hint

Exclude removes the specified type from the union.

Predict Output
advanced
2:00remaining
What is the output of this Exclude with object keys?

What is the resulting type of KeysWithoutId?

Typescript
type Obj = { id: number; name: string; age: number };
type Keys = keyof Obj;
type KeysWithoutId = Exclude<Keys, 'id'>;
A"name" | "age"
B"id" | "name" | "age"
C"id"
Dnever
Attempts:
2 left
💡 Hint

Use keyof to get keys, then exclude 'id'.

Predict Output
advanced
2:00remaining
What error does this code raise?

What error will this code produce?

Typescript
type A = 'x' | 'y';
type B = Exclude<A, 'z'>;

const test: B = 'z';
ANo error, assignment is valid.
BSyntaxError: Unexpected token
CType '"z"' is not assignable to type '"x" | "y"'.
DTypeError: Exclude cannot exclude 'z'
Attempts:
2 left
💡 Hint

Check if the assigned value is part of the type after exclusion.

🧠 Conceptual
expert
2:00remaining
How many items are in the resulting union type?

Given this code, how many string literal types remain in Remaining?

Typescript
type All = 'red' | 'green' | 'blue' | 'yellow' | 'black';
type Remove = 'green' | 'yellow' | 'white';
type Remaining = Exclude<All, Remove>;
A2
B4
C5
D3
Attempts:
2 left
💡 Hint

Count how many items from All are not in Remove.