0
0
Typescriptprogramming~20 mins

Generic conditional constraints in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Generic Constraints Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of generic function with conditional constraint
What is the output of this TypeScript code when calling processValue(10)?
Typescript
function processValue<T extends number | string>(value: T): string {
  if (typeof value === 'number') {
    return `Number: ${value * 2}`;
  } else {
    return `String: ${value.toUpperCase()}`;
  }
}

console.log(processValue(10));
A"String: 10"
BTypeError at runtime
C"Number: 20"
DCompilation error due to generic constraint
Attempts:
2 left
💡 Hint
Check how the function handles numbers differently from strings.
Predict Output
intermediate
2:00remaining
Result of generic function with conditional type check
What will be logged when calling formatValue('hello') in this TypeScript code?
Typescript
function formatValue<T extends string | boolean>(input: T): string {
  return typeof input === 'string' ? input.toUpperCase() : input ? 'TRUE' : 'FALSE';
}

console.log(formatValue('hello'));
ACompilation error due to generic constraint
B"HELLO"
C"TRUE"
D"hello"
Attempts:
2 left
💡 Hint
Look at how the function treats strings differently from booleans.
🔧 Debug
advanced
2:00remaining
Identify the error in generic conditional constraint usage
What error does this TypeScript code produce?
Typescript
function getLength<T>(item: T): number {
  if (item.length) {
    return item.length;
  }
  return 0;
}

getLength(123);
ACompilation error: Generic constraint missing
BReturns 0 without error
CRuntime error: Cannot read property 'length' of undefined
DCompilation error: Property 'length' does not exist on type 'T'.
Attempts:
2 left
💡 Hint
Check if the generic type T is constrained to have a length property.
📝 Syntax
advanced
2:00remaining
Which option correctly defines a generic function with conditional constraint?
Which of the following TypeScript function signatures correctly constrain generic type T to types that have a 'length' property?
Afunction example<T extends { length: number }>(arg: T): number;
Bfunction example<T>(arg: T extends { length: number } ? T : never): number;
Cfunction example<T>(arg: T): T extends { length: number } ? number : never;
Dfunction example<T extends length>(arg: T): number;
Attempts:
2 left
💡 Hint
Look for correct syntax for generic constraints in TypeScript.
🚀 Application
expert
3:00remaining
Determine the output of a complex generic conditional type function
Given the following TypeScript code, what is the output of describeValue([1, 2, 3])?
Typescript
function describeValue<T>(value: T): string {
  type HasLength = T extends { length: number } ? true : false;
  if ((value as any).length !== undefined) {
    return `Length is ${(value as any).length}`;
  } else {
    return 'No length property';
  }
}

console.log(describeValue([1, 2, 3]));
A"Length is 3"
B"No length property"
CCompilation error due to conditional type misuse
DRuntime error: Cannot read property 'length' of undefined
Attempts:
2 left
💡 Hint
Arrays have a length property. The function uses a type alias but checks length at runtime.