Typescript syntax: What the hell is this?

I'm a JS guy looking at an Object in Typescript (it's used in my working project) and I came across this syntax:

myClassFunction: () => void = () => {
    // my function internals here
}

      

I ... honestly don't know what to do about it. What is this syntax, what does it do, what does it mean, and what is it called? (I think it looks like we're assigning an anonymous function to another anonymous function, which shouldn't be, but void is a TS type, so ..... I'm stumped.)

Thank!

+3


source to share


1 answer


There are two parts:

type annotation:

: () => void

This means that this is a function that accepts nothing and returns nothing.

fat arrow function

() => {
    // my function internals here
}

      



This is the function of the ES6 fat arrow. More on this here: http://basarat.gitbooks.io/typescript/content/docs/arrow-functions.html

Conclusion

Personally, I would not comment on this.

var myClassFunction = () => {
    // my function internals here
}

      

And just let typescript infer the type:

enter image description here

+4


source







All Articles