Complete the code to declare a generic function that returns the input value.
function identity<T>(value: [1]): T { return value; }
The generic type parameter T is used as the type of the input value, so the function returns the same type it receives.
Complete the code to create a generic array and push a value into it.
let items: Array<[1]> = []; items.push(42);
The array is declared to hold number types, so pushing 42 (a number) is valid.
Fix the error in the function that tries to access a property of a generic type without constraints.
function getLength<T>(arg: T): number {
return arg.[1];
}The property length is commonly used on arrays and strings. However, without constraints, TypeScript will error because T might not have length. The question asks to fix the error by choosing the property name, so 'length' is correct.
Fill both blanks to constrain the generic type to have a length property and return that length.
function loggingIdentity<T extends [1]>(arg: T): number { return arg.[2]; }
The generic type T is constrained to types that have a length property of type number. Then the function returns arg.length.
Fill all three blanks to create a generic function that returns the keys of an object as an array of strings.
function getObjectKeys<T extends object>(obj: T): Array<[1]> { return Object.[2](obj) as Array<[3]>; }
The function returns the keys of the object as an array of strings. Object.keys returns string[], so the generic type parameter is string and the method is keys.