0
0
Typescriptprogramming~20 mins

Default generic types in Typescript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Default Generic Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of function with default generic type
What is the output of the following TypeScript code?
Typescript
function identity<T = string>(value: T): T {
  return value;
}

const result = identity(123);
console.log(typeof result);
A"undefined"
B"string"
C"object"
D"number"
Attempts:
2 left
💡 Hint
The generic type defaults only if no type is inferred or specified.
Predict Output
intermediate
2:00remaining
Default generic type usage in class
What will be the type of the property value in the following TypeScript class instance?
Typescript
class Box<T = boolean> {
  value: T;
  constructor(value: T) {
    this.value = value;
  }
}

const box = new Box(42);
// What is the type of box.value?
Anumber
Bboolean
Cstring
Dany
Attempts:
2 left
💡 Hint
TypeScript infers generic types from constructor arguments if possible.
🔧 Debug
advanced
2:00remaining
Identify the error with default generic type usage
Which option will cause a TypeScript error when compiling this code?
Typescript
function wrap<T = string>(value: T): { wrapped: T } {
  return { wrapped: value };
}

const result = wrap();
ANo error, result is { wrapped: string } with value undefined
BNo error, result is { wrapped: undefined }
CError: Expected 1 argument, but got 0
DError: Type 'undefined' is not assignable to type 'string'
Attempts:
2 left
💡 Hint
Default generic types do not provide default function arguments.
🧠 Conceptual
advanced
2:00remaining
Effect of default generic types on type inference
Consider the function merge below. What is the type of result when calling merge({a: 1})?
Typescript
function merge<T = { a: number }, U = { b: string }>(obj1: T, obj2?: U): T & U {
  return { ...obj1, ...obj2 } as T & U;
}

const result = merge({ a: 1 });
A{ a: number } & { b: string }
B{ a: number } & undefined
C{ a: number }
DError: Argument of type '{ a: number; }' is not assignable to parameter of type '{ a: number; } & { b: string; }'
Attempts:
2 left
💡 Hint
Optional parameters can be omitted, so U is not inferred and defaults apply.
Predict Output
expert
3:00remaining
Complex default generic types with constraints
What is the output of this TypeScript code when compiled and run with Node.js?
Typescript
function processItem<T extends { id: number } = { id: number; name: string }>(item: T): string {
  return `ID: ${item.id}`;
}

console.log(processItem({ id: 10, name: "Test" }));
console.log(processItem({ id: 20 }));
AID: 10\nError: Property 'name' is missing in second call
BID: 10\nID: 20
CError: Argument of type '{ id: number; }' is not assignable to parameter of type '{ id: number; name: string; }'
DID: 10\nID: undefined
Attempts:
2 left
💡 Hint
Default generic types with constraints allow narrower types to be passed.