identity with a type parameter T.value of type T and return a value of type T.identity with a string and store the result in resultString.identity with a number and store the result in resultNumber.resultString and resultNumber.identity with a type parameter T.value of type T and return a value of type T.identity with a string and store the result in resultString.identity with a number and store the result in resultNumber.resultString and resultNumber.identity with a type parameter T. It should take one parameter value of type T and return the same value.Use the syntax const identity = <T>(value: T): T => value; to create a generic arrow function.
identity with the string "Hello" and store the result in a variable called resultString.Call identity with "Hello" and specify the type string.
identity with the number 42 and store the result in a variable called resultNumber.Call identity with 42 and specify the type number.
resultString and resultNumber using two separate console.log statements.Use console.log(resultString); and console.log(resultNumber); to print the results.
What does the <T> represent in a generic arrow function in TypeScript?
<T> syntax declares a generic type parameter, which acts as a placeholder for any type.Which of the following is the correct syntax for a generic arrow function that returns the same type it receives?
?<T> must come immediately after the function name without an equals sign.T and returns the same type T. const identity = <T>(arg: T): T => arg; matches this syntax correctly.const identity = <T>(arg: T): T => arg; [OK]What will be the output of this TypeScript code?
const wrapInArray = <T>(value: T): T[] => [value];
console.log(wrapInArray(5));
console.log(wrapInArray('hello'));wrapInArray takes a value of any type T and returns an array containing that value.wrapInArray(5) returns [5], and wrapInArray('hello') returns ['hello'].Identify the error in this generic arrow function and choose the corrected version:
const getFirst = <T> = (arr: T[]): T => {
return arr[0];
};<T>, which is not valid syntax for generic arrow functions.const getFirst = <T>(arr: T[]): T => arr[0]; correctly places <T> immediately after the function name without an equals sign and uses concise arrow function syntax.const getFirst = <T>(arr: T[]): T => arr[0]; [OK]Given this generic arrow function, what is the output of mergeObjects({a: 1}, {b: 2})?
const mergeObjects = <T, U>(obj1: T, obj2: U): T & U => ({ ...obj1, ...obj2 });
console.log(mergeObjects({a: 1}, {b: 2}));T and U and returns their intersection type T & U, meaning it combines properties from both.mergeObjects({a: 1}, {b: 2}) returns { a: 1, b: 2 }.Pass the test and this pattern moves unknown