4

I'm just getting started with Angular 2 and TypeScript and I can't seem to figure out how to use callback functions, I know this may be a silly question but given this regular javascript code:

someOnject.doSomething('dsadsaks', function(data){
      console.log(data);
});

What is the equivalent in TypeScript?

Leonardo Jines
  • 360
  • 5
  • 17

2 Answers2

6

The same code works in TypeScript. Alternatively you can use

someOnject.doSomething('dsadsaks', data => {
  console.log(data);
});

The difference is that in the 2nd version this. would refer to the class surrounding the code.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
4

Your example is perfectly valid in a TypeScript project. If you wanted you could also strongly type your inputs:

const msg:string = 'dsadsaks'
someOnject.doSomething(msg, data:string =>{
      console.log(data);
});
agconti
  • 17,780
  • 15
  • 80
  • 114