Nested conditional types let you make decisions inside other decisions in TypeScript. This helps create smart types that change based on many conditions.
Nested conditional types in Typescript
type Result<T> = T extends string ? (T extends 'hello' ? number : boolean) : null;
Nested conditional types use a conditional inside another conditional's true or false branch.
They read like: if T is string, then check if T is 'hello', else boolean; otherwise null.
type Check<T> = T extends number ? (T extends 1 ? 'One' : 'Other number') : 'Not a number';
type Nested<T> = T extends string ? (T extends 'yes' ? true : false) : undefined;
type DeepCheck<T> = T extends object ? (keyof T extends never ? 'Empty object' : 'Has keys') : 'Not object';
This program defines a nested conditional type Describe that checks the type T in steps. It returns different strings based on T's value or type. Then it assigns variables with these types and prints their values.
type Describe<T> = T extends string ? (T extends 'hello' ? 'Greeting' : 'Other string') : T extends number ? (T extends 0 ? 'Zero' : 'Non-zero number') : 'Unknown'; // Test cases let a: Describe<'hello'> = 'Greeting'; let b: Describe<'world'> = 'Other string'; let c: Describe<0> = 'Zero'; let d: Describe<42> = 'Non-zero number'; let e: Describe<boolean> = 'Unknown'; console.log(a); console.log(b); console.log(c); console.log(d); console.log(e);
Nested conditional types can get complex, so keep them readable by formatting well.
They help create very flexible and precise types in TypeScript.
Use them when simple conditional types are not enough.
Nested conditional types let you put one condition inside another.
They help create types that depend on multiple checks.
Use them to make your types smarter and more flexible.
Practice
What does a nested conditional type in TypeScript allow you to do?
Example: type Example<T> = T extends string ? (T extends 'hello' ? number : boolean) : null;
Solution
Step 1: Understand the conditional type structure
The type checks ifTextendsstring. If yes, it checks again ifTextends'hello'.Step 2: Recognize nested conditions
This shows a condition inside another condition, allowing different results based on multiple checks.Final Answer:
Check multiple conditions inside one type to decide the result type -> Option BQuick Check:
Nested conditional types = multiple checks [OK]
- Thinking nested means unrelated types
- Confusing conditional types with functions
- Assuming loops work inside types
Which of the following is the correct syntax for a nested conditional type in TypeScript?
type Result<T> = T extends number ? (T extends 5 ? 'Five' : 'Other') : 'Not a number';
Solution
Step 1: Check the syntax of nested conditional types
Nested conditional types use the syntaxcondition ? (nested condition ? trueType : falseType) : elseType.Step 2: Identify correct parentheses and keywords
type Result<T> = T extends number ? (T extends 5 ? 'Five' : 'Other') : 'Not a number'; correctly uses parentheses and the ternary?and:operators. Options A, B, and D have syntax errors or invalid keywords.Final Answer:
type Result<T> = T extends number ? (T extends 5 ? 'Five' : 'Other') : 'Not a number'; -> Option DQuick Check:
Correct nested syntax = type Result<T> = T extends number ? (T extends 5 ? 'Five' : 'Other') : 'Not a number'; [OK]
- Omitting parentheses around nested condition
- Using if/then syntax inside types
- Using braces {} instead of parentheses
What is the resulting type of CheckType<"hello"> given this code?
type CheckType<T> = T extends string ? (T extends "hello" ? number : boolean) : null;
Solution
Step 1: Evaluate outer condition
Tis"hello", which extendsstring, so the outer condition is true.Step 2: Evaluate inner condition
Now check ifTextends"hello". It does, so the inner condition returnsnumber.Final Answer:
number -> Option CQuick Check:
"hello" extends "hello" = number [OK]
- Ignoring inner condition and picking boolean
- Choosing null because of outer else
- Confusing string literal with string type
Identify the error in this nested conditional type:
type Test<T> = T extends number ? T extends 10 ? 'Ten' : 'Other' : 'Not a number'
Solution
Step 1: Check nested conditional syntax
The nested conditionalT extends 10 ? 'Ten' : 'Other'must be wrapped in parentheses to group it properly.Step 2: Identify missing parentheses impact
Without parentheses, TypeScript cannot parse the nested condition correctly, causing a syntax error.Final Answer:
Missing parentheses around the nested conditional -> Option AQuick Check:
Nested condition needs parentheses [OK]
- Forgetting parentheses causes syntax errors
- Thinking semicolon is required inside type
- Misusing extends keyword
Given this nested conditional type, what is the type of Result<string | number>?
type Result<T> = T extends string ? (T extends 'a' ? 1 : 2) : (T extends number ? 3 : 4);
Solution
Step 1: Distribute conditional over union types
ForT = string | number, TypeScript applies the conditional to each union member separately.Step 2: Evaluate each branch
Forstring: if'a'then1else2. Fornumber:3. So combined type is1 | 2 | 3.Final Answer:
1 | 2 | 3 -> Option AQuick Check:
Union splits nested condition = 1|2|3 [OK]
- Including 4 which is for non-string/number
- Not splitting union types correctly
- Assuming single output type for union
Ready to prove it?
Pass the test and this pattern moves unknown
