我正在学习 TypeScript。今天我在网上随机打开一个项目,在一个 TypeScript 文件中看到了这个:
import { Route, useRoute } from "@react-navigation/native"
import { NewAppointmentConfirmationScreenProps } from "@app/screens/new-appointment-confirmation/types"
export const useNewAppointmentConfirmationScreenRoute = () =>
useRoute<Route<"NewAppointmentConfirmationScreen", NewAppointmentConfirmationScreenProps>>()
我从来没有见过这个功能!这是什么:
useRoute<Route<"NewAppointmentConfirmationScreen", NewAppointmentConfirmationScreenProps>>()
是什么意思?我知道组件类似于 <abc/>
但是什么是函数名后跟尖括号(小于/大于符号)?
最佳答案
它被称为泛型
https://www.typescriptlang.org/docs/handbook/2/generics.html
了解更多具体信息
简而言之,当我们需要使用不同语法的类的强类型时,它可以帮助我们
一段视频 generics video
我不确定,但我认为从历史上看,这是来自强类型语言 在 java 和 c# 中更常见
例如当我们在java中初始化一个arrayList时 我们需要指定我们存储在三角括号中的数据类型,就像您在示例中指定的那样
https://www.w3schools.com/java/java_arraylist.asp
你看到语法了吗?
ArrayList<String>
如果我是正确的,那么它只是函数参数类型
***编辑
我刚刚在 .net5 页面上看到了这个
https://learn.microsoft.com/en-us/dotnet/api/system.func-2?view=net-5.0
查看并在线使用运行时
关于typescript - TypeScript 中函数名称后面的 <abc<xyz>> 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68418310/