当前位置:网站首页>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;
//}边栏推荐
- Daily question 1: force deduction: 225: realize stack with queue
- Classification, function and usage of MySQL constraints
- 小程序系统更新提示,并强制小程序重启并使用新版本
- Attack and defense world ditf Misc
- Management background --2 Classification list
- Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem
- Dealing with the crash of QT quick project in offscreen mode
- 剑指offer刷题记录1
- 做国外LEAD2022年下半年几点建议
- 2022-07-05 使用tpcc对stonedb进行子查询测试
猜你喜欢

0 basic learning C language - digital tube
![[线性代数] 1.3 n阶行列式](/img/6e/54f3a994fc4c2c10c1036bee6715e8.gif)
[线性代数] 1.3 n阶行列式

Adavit -- dynamic network with adaptive selection of computing structure
![[leetcode daily clock in] 1020 Number of enclaves](/img/2d/3d12f20c8c73fb28044c01be633c99.jpg)
[leetcode daily clock in] 1020 Number of enclaves

Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing

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

labelimg的安装与使用

新手程序员该不该背代码?
The SQL response is slow. What are your troubleshooting ideas?

【LeetCode】19、 删除链表的倒数第 N 个结点
随机推荐
Notes de développement du matériel (10): flux de base du développement du matériel, fabrication d'un module USB à RS232 (9): création de la Bibliothèque d'emballage ch340g / max232 SOP - 16 et Associa
每日一题:力扣:225:用队列实现栈
MySQL约束的分类、作用及用法
The ceiling of MySQL tutorial. Collect it and take your time
Aardio - 不声明直接传float数值的方法
Mise en place d'un environnement de développement OP - tee basé sur qemuv8
[leetcode] 19. Delete the penultimate node of the linked list
(十八)LCD1602实验
Config:invalid signature solution and troubleshooting details
NPDP认证|产品经理如何跨职能/跨团队沟通?
go多样化定时任务通用实现与封装
自制J-Flash烧录工具——Qt调用jlinkARM.dll方式
Should novice programmers memorize code?
Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)
网络基础入门理解
Assembly and interface technology experiment 5-8259 interrupt experiment
如何实现文字动画效果
软考高级(信息系统项目管理师)高频考点:项目质量管理
SQL server generates auto increment sequence number
Export MySQL table data in pure mode