Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get name of function in typescript

I was looking for a way to get name of function passing in parameter

console.clear();
class A{
   test(){


   }
   testCall(fnc:Function){
     console.log(fnc.name); // i want it display test here not empty
     console.log(fnc);

   }
}

var a=new A();
a.testCall(a.test);

you can check this in jsbin http://jsbin.com/loluhu/edit?js,console

like image 749
Mr.Trieu Avatar asked May 20 '16 08:05

Mr.Trieu


2 Answers

I found this is a bug in typescript

you can find solution here TypeScript not providing function name

like image 132
Mr.Trieu Avatar answered Oct 01 '22 06:10

Mr.Trieu


You can extend the Function interface as follows:

interface Function {
    name: string;
}

function foo() {}
alert(foo.name);

See here for a fuller explanation.

like image 35
Beevik Avatar answered Oct 01 '22 06:10

Beevik