⊗tsSpGnInt 33 of 37 menu

Generic Type Interface in TypeScript

Now let's move on to the description of the generic type interface for calling the function. Let's define the function call signature in it:

interface IFunc { <T> (data: T): T; }

Next, we declare the function myFunc and write the code in its body:

function myFunc <T> (data: T): T { return data; } let func: IFunc = myFunc;

Then we write a variable func, whose type refers to IFunc. And the variable itself calls the function myFunc:

let func: IFunc = myFunc;

The full code will look like this:

interface IFunc { <T> (data: T): T; } function myFunc <T> (data: T): T { return data; } let func: IFunc = myFunc; console.log(func('abcde'));

After running the code we will see:

'abcde'
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline