Conditional types let you choose one type or another based on a condition. It helps make your code smarter and safer.
Conditional type syntax in Typescript
type ResultType<T> = T extends SomeType ? TypeIfTrue : TypeIfFalse;The extends keyword here means 'if T fits into SomeType'.
If the condition is true, the type becomes TypeIfTrue, otherwise TypeIfFalse.
T is a string. If yes, the type is "Yes", else "No".type IsString<T> = T extends string ? "Yes" : "No";
T is an array, it returns the element type, else T itself.type ElementType<T> = T extends (infer U)[] ? U : T;null and undefined from a type.type NonNullable<T> = T extends null | undefined ? never : T;
This program defines a conditional type CheckType that returns "Number" if the type is number, otherwise "Other". Then it shows how to use it with variables and prints the results.
type CheckType<T> = T extends number ? "Number" : "Other"; let a: CheckType<number> = "Number"; let b: CheckType<string> = "Other"; console.log(a); console.log(b);
Conditional types are checked at compile time, so they don't affect runtime performance.
You can combine conditional types with other TypeScript features like infer to extract types.
Conditional types let you pick types based on conditions.
They use extends with a question mark and colon to choose between two types.
They help make your code flexible and safe by adapting types automatically.
Practice
T extends U ? X : YSolution
Step 1: Understand the conditional type syntax
The syntaxT extends U ? X : Ymeans if type T can be assigned to type U, then use type X.Step 2: Interpret the meaning
If T is not assignable to U, then the type becomes Y instead.Final Answer:
Chooses type X if T is assignable to U, otherwise type Y -> Option BQuick Check:
Conditional type picks between X and Y based on T extends U [OK]
- Thinking it returns a boolean value
- Confusing extends with equality check
- Assuming it creates a function
Solution
Step 1: Recall the correct conditional type syntax
TypeScript usestype Result = T extends U ? X : Y;to define conditional types.Step 2: Identify incorrect options
Options A, B, and C use invalid syntax not supported by TypeScript.Final Answer:
type Result = T extends U ? X : Y; -> Option CQuick Check:
Correct conditional type syntax uses ? and : [OK]
- Using if-else keywords inside type definitions
- Misplacing question mark or colon
- Writing 'then' instead of '?'
type Check<T> = T extends string ? number : boolean;
type Result = Check<'hello'>;
Solution
Step 1: Analyze the conditional type Check<T>
If T extends string, then the type is number; otherwise boolean.Step 2: Substitute T with 'hello'
'hello' is a string literal, so it extends string, making the type number.Final Answer:
number -> Option AQuick Check:
'hello' extends string -> number [OK]
- Confusing the output type with input type
- Assuming literal types don't extend their base types
- Choosing boolean instead of number
type Example<T> = T extends number ? 'num' : 'not num'
Solution
Step 1: Review the conditional type syntax
The syntaxtype Example<T> = T extends number ? 'num' : 'not num'is valid in TypeScript.Step 2: Check for syntax errors
Semicolon after type alias is optional; string literals are valid return types.Final Answer:
No error, syntax is correct -> Option DQuick Check:
Conditional types can return string literals [OK]
- Expecting semicolon is mandatory
- Thinking extends is only for classes
- Believing string literals can't be types
type Flatten<T> = T extends any[] ? T[number] : T;
What is the type of
Flatten<string[] | number>?Solution
Step 1: Understand the conditional type Flatten<T>
If T is an array type, it returns the element type (T[number]), else returns T itself.Step 2: Apply Flatten to union type string[] | number
Conditional types distribute over unions, so Flatten<string[] | number> = Flatten<string[]> | Flatten<number> = string | number.Final Answer:
string | number -> Option AQuick Check:
Conditional types distribute over unions [OK]
- Not distributing over union types
- Returning array type instead of element type
- Confusing T[number] with T
Ready to prove it?
Pass the test and this pattern moves unknown
