Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

generic in typescript

/*
What are Generics?
Generics have been a major feature of strongly typed languages 
like Java and C#. In TypeScript, they allow the types of components 
and functions to be "SPECIFIED LATER" which allows them to be used 
in creating reusable components that can apply to different use cases, 

for example:
*/

function returnInput <Type>(arg: Type): Type {
  return arg;
};
const returnInputStr = returnInput<string>('Foo Bar');
const returnInputNum = returnInput<number>(5);

console.log(returnInputStr); // Foo Bar
console.log(returnInputNum); // 5
Source by blog.openreplay.com #
 
PREVIOUS NEXT
Tagged: #generic #typescript
ADD COMMENT
Topic
Name
9+3 =