[英][Syntax Help]What is "variable : () => " meaning in Typescript or JavaScript
I was reading a source code about next-firebase-auth.我正在阅读有关 next-firebase-auth 的源代码。
I am confused about the following code syntax我对以下代码语法感到困惑
export const useAuthUser: () => AuthUserContext
export const verifyIdToken: (token: string) => Promise<AuthUser>
The right side look like an arrow function.右侧看起来像一个箭头函数。 The left side is declaring varibles.
左边是声明变量。 Intuitively, I am thinking is the same as the following:
直觉上,我认为与以下相同:
export const useAuthUser = () => AuthUserContext
However, it doesn't.然而,事实并非如此。 I did some test on Chrome console.
我在 Chrome 控制台上做了一些测试。 Here is the code
这是代码
adder : () => (a + b)
When I hit enter, it shows当我按回车时,它显示
() => (a+b)
But If I type adder, it shows但是如果我输入加法器,它会显示
Uncaught ReferenceError: adder is not defined
I looked other posts here.我在这里看了其他帖子。 It seems like the colon (:) is related to type annotation .
似乎冒号 (:) 与类型注释有关。 So does it mean the right side is a type or interface?
那么这是否意味着右侧是类型或接口? But how is an arrow function type or interface?
但是箭头函数类型或接口如何? It does not make sense.
它没有任何意义。
Thanks for your help!谢谢你的帮助!
The stuff after the colon is the type.冒号后面的东西是类型。 As a type,
() => AuthUserContext
means a function that doesn't require any arguments, and which returns an AuthUserContext
.作为一种类型,
() => AuthUserContext
表示不需要任何参数并返回AuthUserContext
的函数。 (token: string) => Promise<AuthUser>
Means a function that requires a single argument which is a string, and then returns a Promise that will resolve to an AuthUser
(token: string) => Promise<AuthUser>
表示需要一个字符串参数的函数,然后返回一个 Promise 将解析为AuthUser
The lines of code you showed seem incomplete.您显示的代码行似乎不完整。 Typically, a const will immediately followed by an assignment, as in:
通常,一个 const 将紧跟一个赋值,如下所示:
export const useAuthUser: () => AuthUserContext = () => {
// some code which returns an AuthUserContext
}
I did some test on Chrome console.
我在 Chrome 控制台上做了一些测试。
The console only supports javascript, not typescript控制台仅支持 javascript,不支持 typescript
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.