0
0
Typescriptprogramming~20 mins

Generic constraints with extends 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 extends constraint
What is the output of the following TypeScript code?
Typescript
function getLength<T extends { length: number }>(arg: T): number {
  return arg.length;
}

console.log(getLength('hello'));
console.log(getLength([1, 2, 3]));
ATypeError at runtime
B5\n3
Chello\n[1,2,3]
DCompilation error
Attempts:
2 left
💡 Hint
Remember that both strings and arrays have a length property.
🧠 Conceptual
intermediate
1:30remaining
Understanding extends in generic constraints
Which statement best describes the role of extends in the generic constraint T extends { length: number }?
AIt restricts T to types that have a length property of type number.
BIt makes T inherit properties from a class named length.
CIt allows T to be any type without restrictions.
DIt forces T to be exactly the type { length: number }.
Attempts:
2 left
💡 Hint
Think about what types can be passed to a function requiring a length property.
🔧 Debug
advanced
2:30remaining
Identify the error in generic constraint usage
What error does this TypeScript code produce?
Typescript
function copyFields<T, U extends T>(target: T, source: U): T {
  for (const id in source) {
    target[id] = source[id];
  }
  return target;
}

const x = { a: 1, b: 2 };
const y = { a: 1, b: 3, c: 4 };
copyFields(x, y);
AError: Cannot assign to 'target[id]' because it is a constant.
BNo error, code runs fine.
CError: Type 'U' is not assignable to type 'T'.
DError: Property 'c' does not exist on type 'T'.
Attempts:
2 left
💡 Hint
Check the properties of the objects and the generic constraints.
📝 Syntax
advanced
2:00remaining
Correct generic constraint syntax
Which option correctly declares a generic function that accepts only objects with a name property of type string?
Afunction greet<T extends { name: string }>(obj: T): string { return `Hello, ${obj.name}`; }
Bfunction greet<T>(obj: T extends { name: string }): string { return `Hello, ${obj.name}`; }
Cfunction greet<T>(obj: T) where T has name: string { return `Hello, ${obj.name}`; }
Dfunction greet<T: { name: string }>(obj: T): string { return `Hello, ${obj.name}`; }
Attempts:
2 left
💡 Hint
Remember the correct syntax for generic constraints in TypeScript.
🚀 Application
expert
3:00remaining
Determine the output of complex generic constraint usage
What is the output of this TypeScript code?
Typescript
interface Lengthwise {
  length: number;
}

function loggingIdentity<T extends Lengthwise>(arg: T): T {
  console.log(arg.length);
  return arg;
}

const result = loggingIdentity({ length: 10, value: 3 });
console.log(result.value);
ACompilation error: Property 'value' does not exist on type 'T'.
B10\nundefined
C10\n3
DRuntime error: Cannot read property 'value' of undefined.
Attempts:
2 left
💡 Hint
Consider how TypeScript infers the type of the argument and the return value.