1

I'm relatively new to JS/TS so please excuse my lack of knowing the names of these two types of functions, if I did I probably wouldn't have to create a new question here.

But I wanted to know what the difference is behind these two functions:

const doubleNumber: (i: number) => number = (i: number) => {
    return i * 2;
};

const doubleNumber2 = (i: number) => {
    return i * 2;
};

Apologies for the (most likely) duplicate question.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
Azhari
  • 167
  • 13
  • 2
    Either of the examples is not a function declaration. – Teemu Jul 02 '19 at 08:07
  • 1
    @T.J.Crowder oh, I misread the first declaration as `a = b => c => b` – VLAZ Jul 02 '19 at 08:09
  • 2
    Just to explain @Teemu's comment: Both of those are function *expressions* being assigned to a constant, not function *declarations*. I go through the details of the differences in [this answer](https://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname/22173438#22173438). – T.J. Crowder Jul 02 '19 at 08:14

1 Answers1

4

There is no difference in the function itself. Just the first one is explicitly type hinted ((i: number) => number, meaning a function that takes one number and returns one number), whereas the second one omits the type hint.

const doubleNumber: (i: number) => number = (i: number) => {
//                  ^^^^^^^^^^^^^^^^^^^^^ ------------------- This is the type hint
    return i * 2;
};
T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
deceze
  • 471,072
  • 76
  • 664
  • 811
  • ^^ The type hint in this case being completely unnecessary, TypeScript will infer the type correctly. – T.J. Crowder Jul 02 '19 at 08:12
  • I still don't understand why there's two "i: number" declarations? – Azhari Jul 02 '19 at 08:12
  • 2
    @Azhari One is between `:` and `=`, that's the type hint. It looks very redundant since the type hint for "function that takes a number and returns a number" looks very much identical to an actual function. – deceze Jul 02 '19 at 08:13
  • Ahh I understand this now! Thank you :) – Azhari Jul 02 '19 at 08:16
  • 1
    @Azhari - This [playground link](http://www.typescriptlang.org/play/index.html#code/PTAEBUAsEsGcC4BQBjA9gO1gF1AE1QK4BGANgKYByBAtkWQE7ygAU0T6Nd9AlKALwA+UB1oN+LNsM4Neg0AG9EoZaHpksBeulDRQAKlAAmANyIAvqZA7YoLJDKhYAQ2oOnCRORz5i5KqPpDJlZ2aR5+IREuSzAVOPi4gD1klNS09IBaLNt7WwBPAAcHVAAzHIcANyd6aCdSMkQfev8uQ3EQqQDZIUU4tQ0tHX0jUwtEKwA6KbIAD2QyApxS0DRNWAcS1Hpy0AADNEwsXeBdr128aBKShjJ0eaA) may also help. Happy coding! – T.J. Crowder Jul 02 '19 at 08:17
  • Do either of those functions have particular names? I'd like to change the title to them so it's a bit more clearer what I'm trying to compare. – Azhari Jul 02 '19 at 08:22
  • 1
    @Azhari not really. As **deceze** said, the only difference is that the first has the type explicitly set. There is no special name for that, to my knowledge. You can call it "explicitly typed", if you wish and people should get what you mean but I don't think there is "official" terminology. – VLAZ Jul 02 '19 at 08:25