这两种方式为箭头函数定义类型之间有什么区别?

问题描述

我对以下两种虚拟箭头功能间的区别感到困惑。有人可以指出其中的区别吗?

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

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

解决方法

第二个函数抛出错误!

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

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

因为hello函数类型不是字符串。

您好函数类型为“((i:字符串)=>字符串”)。

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