Complete the code to declare a generic function that returns the input value.
function identity<T>([1]: T): T { return value; }
The generic function uses value as the parameter name to return it directly.
Complete the code to define a generic interface with a property of type T.
interface Box<[1]> {
contents: T;
}The generic type parameter is conventionally named T and used inside the interface.
Fix the error in the generic function to constrain T to types with a length property.
function logLength<T [1] { length: number }>(arg: T): T { console.log(arg.length); return arg; }
The extends keyword constrains the generic type to types that have a length property.
Fill both blanks to create a generic function that returns the first element of an array of type T.
function firstElement<[1]>(arr: [2][]): [1] { return arr[0]; }
The generic type parameter T is used both to declare the generic and the array element type.
Fill all three blanks to create a generic function that maps an array of type T to an array of type U using a callback.
function mapArray<[1], [2]>(arr: [1][], callback: (item: [1]) => [2]): [2][] { return arr.map(callback); }
The generic types T and U represent input and output types respectively, used consistently in parameters and return type.