Nice Guy
Nice Guy

Reputation: 165

What is the difference between defining types for arrow function in these two ways?

I'm confused about what the difference between these two the following types of a dummy arrow function. Can someone please point out the difference?

const hello = (i: string):string => { return "Hi, User!" }

and

const hello: string = (i: string) => { return "Hi, User!" }

Upvotes: 1

Views: 48

Answers (1)

hyundeock
hyundeock

Reputation: 505

second function throw error!

[yours]
1. const hello = (i: string):string => { return "Hi, User!" } // correct

2. const hello: string = (i: string) => { return "Hi, User!" } // error

Because hello function type is not string.

hello function type is "(i: string) => string".

[correct]
const hello: (i:string) => string = (i: string) => { return "Hi, User!" }

Upvotes: 2

Related Questions