当前位置:网站首页>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;
//}边栏推荐
- 中国1,4-环己烷二甲醇(CHDM)行业调研与投资决策报告(2022版)
- 12、 Start process
- MySQL教程的天花板,收藏好,慢慢看
- RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv
- C# 三种方式实现Socket数据接收
- Comparison between variable and "zero value"
- AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
- Daily question 1: force deduction: 225: realize stack with queue
- Inno setup packaging and signing Guide
- 变量与“零值”的比较
猜你喜欢

剑指offer刷题记录1

Senior soft test (Information System Project Manager) high frequency test site: project quality management

CocosCreator+TypeScripts自己写一个对象池

signed、unsigned关键字

Aardio - 封装库时批量处理属性与回调函数的方法

基於 QEMUv8 搭建 OP-TEE 開發環境

Attack and defense world ditf Misc

剪映+json解析将视频中的声音转换成文本

Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题

3DMAX assign face map
随机推荐
Installation and use of labelimg
如何用程序确认当前系统的存储模式?
extern关键字
【编译原理】做了一半的LR(0)分析器
Gd32f4xx serial port receive interrupt and idle interrupt configuration
中国VOCs催化剂行业研究与投资战略报告(2022版)
基於 QEMUv8 搭建 OP-TEE 開發環境
go多样化定时任务通用实现与封装
What are the interface tests? What are the general test points?
【LeetCode】19、 删除链表的倒数第 N 个结点
AdaViT——自适应选择计算结构的动态网络
return 关键字
将MySQL的表数据纯净方式导出
PVL EDI project case
二分图判定
The ceiling of MySQL tutorial. Collect it and take your time
CocosCreator+TypeScripts自己写一个对象池
枚举与#define 宏的区别
雅思口语的具体步骤和时间安排是什么样的?
Data processing skills (7): MATLAB reads the data in the text file TXT with mixed digital strings