Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

typescript generic function

function firstElement<Type>(arr: Type[]): Type {
  return arr[0];
}
// s is of type 'string'
const s = firstElement(["a", "b", "c"]);
// n is of type 'number'
const n = firstElement([1, 2, 3]);
Source by blog.openreplay.com #
 
PREVIOUS NEXT
Tagged: #typescript #generic #function
ADD COMMENT
Topic
Name
4+3 =