Bird
Raised Fist0
Typescriptprogramming~10 mins

Why conditional types are needed in Typescript - Visual Breakdown

Choose your learning style10 modes available
Concept Flow - Why conditional types are needed
Start with generic type
↓
Check condition on type
↓
Return one
↓
Final type
Conditional types check a type condition and choose one type if true, another if false, helping create flexible and precise types.
Execution Sample
Typescript
type IsString<T> = T extends string ? "Yes" : "No";

type A = IsString<string>;
type B = IsString<number>;
This code defines a conditional type that returns "Yes" if the input type is string, otherwise "No".
Execution Table
StepType Parameter TCondition (T extends string?)Result Type
1stringtrue"Yes"
2numberfalse"No"
💡 All type parameters checked, conditional types resolved to final types.
Variable Tracker
VariableStartAfter 1After 2Final
Tgenericstringnumberresolved
Result Typeunknown"Yes""No"final
Key Moments - 2 Insights
Why do we need conditional types instead of simple unions?
Conditional types let us pick different types based on input, unlike unions which combine types without selection. See execution_table rows 1 and 2 where different inputs produce different outputs.
What does 'T extends string ? "Yes" : "No"' mean?
It means: if T is assignable to string, use "Yes", else use "No". This is shown in execution_table where condition is true for string and false for number.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Result Type when T is number?
A"Yes"
B"No"
Cnumber
Dstring
💡 Hint
Check execution_table row 2 under Result Type column.
At which step does the condition 'T extends string' evaluate to true?
AStep 1
BStep 2
CBoth steps
DNeither step
💡 Hint
Look at execution_table Condition column for each step.
If we change T to boolean, what would the Result Type be?
A"Yes"
Bboolean
C"No"
Dunknown
💡 Hint
Refer to the pattern in execution_table where non-string types result in "No".
Concept Snapshot
Conditional types syntax: T extends U ? X : Y
They let types choose between X or Y based on whether T fits U.
Useful for flexible, precise typing.
Example: IsString<T> returns "Yes" if T is string, else "No".
Full Transcript
Conditional types in TypeScript let us create types that depend on other types. We start with a generic type T. Then we check if T fits a condition, like being a string. If yes, we pick one type; if no, another. For example, IsString<T> returns "Yes" if T is string, else "No". This helps make types smarter and more flexible than simple unions. The execution table shows how for T=string, the result is "Yes", and for T=number, it is "No". This way, conditional types help us write clearer and safer code.

Practice

(1/5)
1. Why do we need conditional types in TypeScript?
type Result<T> = T extends number ? string : boolean;
easy
A. To create loops in type definitions
B. To declare variables without types
C. To run code only if a condition is true
D. To select a type based on a condition, making code flexible

Solution

  1. Step 1: Understand conditional types purpose

    Conditional types allow choosing one type or another based on a condition, like T extends number ? string : boolean.
  2. Step 2: Match the purpose with options

    To select a type based on a condition, making code flexible correctly describes this as selecting a type based on a condition for flexibility.
  3. Final Answer:

    To select a type based on a condition, making code flexible -> Option D
  4. Quick Check:

    Conditional types choose types conditionally = D [OK]
Hint: Conditional types pick types based on conditions [OK]
Common Mistakes:
  • Confusing conditional types with runtime if statements
  • Thinking conditional types declare variables
  • Believing conditional types create loops
2. Which of the following is the correct syntax for a conditional type in TypeScript?
easy
A. type Check<T> = T extends string ? number : boolean;
B. type Check<T> = if T then string else number;
C. type Check<T> = T ? string : number;
D. type Check<T> = T == string ? number : boolean;

Solution

  1. Step 1: Recall conditional type syntax

    The correct syntax uses extends to check type relations: T extends U ? X : Y.
  2. Step 2: Identify correct option

    type Check<T> = T extends string ? number : boolean; matches this syntax exactly. Options A, B, and D use invalid syntax for types.
  3. Final Answer:

    type Check<T> = T extends string ? number : boolean; -> Option A
  4. Quick Check:

    Conditional type syntax uses extends = C [OK]
Hint: Look for 'extends' keyword in conditional types [OK]
Common Mistakes:
  • Using runtime if/else syntax in types
  • Using == instead of extends
  • Omitting the extends keyword
3. What is the output type of this code?
type Check<T> = T extends string ? number : boolean;
type Result = Check<'hello'>;
medium
A. string
B. number
C. boolean
D. any

Solution

  1. Step 1: Evaluate the conditional type with T = 'hello'

    Since 'hello' is a string, T extends string is true.
  2. Step 2: Determine the resulting type

    Because the condition is true, the type becomes number.
  3. Final Answer:

    number -> Option B
  4. Quick Check:

    'hello' extends string = number [OK]
Hint: Check if type matches condition to pick true branch [OK]
Common Mistakes:
  • Confusing the true and false branches
  • Thinking the output is string literal
  • Assuming any or unknown type
4. Identify the error in this conditional type:
type Check<T> = T extends number ? 'num' : 'not num';
type Result = Check<>;
medium
A. Wrong use of extends keyword
B. Conditional types cannot return string literals
C. Missing type argument in Check<>
D. Result type must be declared with let or const

Solution

  1. Step 1: Check usage of generic type parameter

    The type alias Check requires a type argument, but Check<> is missing it.
  2. Step 2: Confirm other parts are correct

    The extends keyword and string literal return types are valid. Declaring a type does not use let or const.
  3. Final Answer:

    Missing type argument in Check<> -> Option C
  4. Quick Check:

    Generic types need arguments = B [OK]
Hint: Always provide type arguments for generic types [OK]
Common Mistakes:
  • Leaving generic type arguments empty
  • Confusing type aliases with variables
  • Thinking extends is invalid in types
5. Given this conditional type, what is the type of Flattened?
type Flatten<T> = T extends Array ? U : T;
type Flattened = Flatten;
hard
A. number | string
B. number[] | string
C. Array
D. never

Solution

  1. Step 1: Analyze the conditional type with union input

    The input is number[] | string. Conditional types distribute over unions, so it applies separately to number[] and string.
  2. Step 2: Apply Flatten to each part

    For number[], it matches Array<infer U> with U = number, so result is number.
    For string, it does not match array, so result is string.
  3. Step 3: Combine results

    The combined result is number | string.
  4. Final Answer:

    number | string -> Option A
  5. Quick Check:

    Conditional types distribute over unions = A [OK]
Hint: Remember conditional types distribute over union types [OK]
Common Mistakes:
  • Not distributing over union types
  • Returning array instead of element type
  • Confusing infer usage
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