Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typescript: equivalent way of declaring function type?

Tags:

typescript

let f1: {(): number}

let f2: () => number

let f3: {() => number} // error TS1005: ':' expected.

It looks like f1 and f2 declarations are equivalent, is it true?

And why f3 is an error?

like image 660
attdona Avatar asked Jan 22 '26 00:01

attdona


1 Answers

The notation with the braces allows you to define overload method signatures and/or hybrid types, for example

interface Foo {
    (x: number): void,
    (x: string): void,
    bar: string,
}

has two call signatures and one property bar.

If you only have one call signature and no properties you can use the shorthand syntax you used for f2. So with the braces you have to separate the arguments from the return type with : and in the shorthand syntax with =>.

like image 136
Tao Avatar answered Jan 25 '26 15:01

Tao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!