当前位置:网站首页>TypeScript获取函数参数类型
TypeScript获取函数参数类型
2022-07-06 15:10:00 【用户6256742】
现在有一个函数update,我们想要获取他的参数类型,你应该怎么做呢?这个时候我们需要就要用到Parameters
function updata(state) {
return {
router: state.router
}
}
获取参数类型:
type ArrType = Parameters<typeof state>
// ArrType => [state: any]
如果想获取state的类型呢?这个时候需要用到infer
type GetType<T> = T extends (arg: infer P) => void ? P : string;
type StateType = GetType<typeof state>
// StateType => any
// 因为state没有设置类型,所以ts推断state的类型为any
把这段代码翻译一下: (arg: infer P):arg的类型待推断为P 整段代码的意思:如果T能赋值给(arg: infer P) => void,则返回P,否则返回string
如果想要获取函数的返回值类型,需要使用typescript提供的内置方法ReturnType
type Return = ReturnType<typeof state>
// ReturnType =>
// {
// router: any;
//}
边栏推荐
- Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings
- Management background --2 Classification list
- Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing
- 第3章:类的加载过程(类的生命周期)详解
- i. Mx6ull build boa server details and some of the problems encountered
- Web APIs DOM time object
- MySQL约束的分类、作用及用法
- Comparison between variable and "zero value"
- PVL EDI project case
- Aardio - 封装库时批量处理属性与回调函数的方法
猜你喜欢
Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
【数字IC手撕代码】Verilog无毛刺时钟切换电路|题目|原理|设计|仿真
[leetcode daily clock in] 1020 Number of enclaves
机试刷题1
Chapter 4: talk about class loader again
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
新手程序员该不该背代码?
云原生技术--- 容器知识点
Aardio - 通过变量名将变量值整合到一串文本中
Attack and defense world ditf Misc
随机推荐
case 关键字后面的值有什么要求吗?
二叉(搜索)树的最近公共祖先 ●●
雅思口语的具体步骤和时间安排是什么样的?
2022-07-04 mysql的高性能数据库引擎stonedb在centos7.9编译及运行
2022-07-05 使用tpcc对stonedb进行子查询测试
Balanced Multimodal Learning via On-the-fly Gradient Modulation(CVPR2022 oral)
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
BasicVSR_PlusPlus-master测试视频、图片
Attack and defense world miscall
小程序系统更新提示,并强制小程序重启并使用新版本
SQL Server生成自增序号
[sdx62] wcn685x will bdwlan Bin and bdwlan Txt mutual conversion operation method
ThreadLocal详解
Clip +json parsing converts the sound in the video into text
PVL EDI project case
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
自定义 swap 函数
RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv
如何用程序确认当前系统的存储模式?
[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation