Complete the code to define a generic function with a default type.
function identity<T = [1]>(arg: T): T { return arg; }
The default generic type is any, so if no type is specified, T defaults to any.
Complete the code to create a generic class with a default type.
class Box<T = [1]> { contents: T; constructor(value: T) { this.contents = value; } }
number or boolean when the example expects string.The default generic type is string, so if no type is specified, T defaults to string.
Fix the error in the generic function by adding a default type.
function wrapValue<T[1]>(value: T): T[] { return [value]; }
extends instead of = for default types.Adding = string sets the default generic type to string, fixing the error if no type is provided.
Fill both blanks to create a generic interface with a default type and a property using that type.
interface Container<T = [1]> { value: [2]; }
The default generic type is string, and the property value uses the generic type T.
Fill all three blanks to define a generic function with two type parameters, one with a default, and return a tuple.
function pair<T, U = [1]>(first: T, second: U): [[2], [3]] { return [first, second]; }
The second generic type U defaults to number. The return tuple uses types T and U.