当前位置:网站首页>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;
//}
边栏推荐
- 2022-07-04 the high-performance database engine stonedb of MySQL is compiled and run in centos7.9
- Advantages of link local address in IPv6
- 变量与“零值”的比较
- 图像的spatial domain 和 frequency domain 图像压缩
- ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
- What are the specific steps and schedule of IELTS speaking?
- Applet system update prompt, and force the applet to restart and use the new version
- 机试刷题1
- Solve project cross domain problems
- 剑指offer刷题记录1
猜你喜欢
Management background --4, delete classification
关于声子和热输运计算中BORN电荷和non-analytic修正的问题
Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)
Improving Multimodal Accuracy Through Modality Pre-training and Attention
手写ABA遇到的坑
Advantages of link local address in IPv6
config:invalid signature 解决办法和问题排查详解
[leetcode daily clock in] 1020 Number of enclaves
PVL EDI 项目案例
CocosCreator+TypeScripts自己写一个对象池
随机推荐
中国VOCs催化剂行业研究与投资战略报告(2022版)
Lora sync word settings
第4章:再谈类的加载器
RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv
Gd32f4xx serial port receive interrupt and idle interrupt configuration
Pit encountered by handwritten ABA
[leetcode daily clock in] 1020 Number of enclaves
2021 geometry deep learning master Michael Bronstein long article analysis
Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"
The ceiling of MySQL tutorial. Collect it and take your time
case 关键字后面的值有什么要求吗?
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
[linear algebra] determinant of order 1.3 n
Adavit -- dynamic network with adaptive selection of computing structure
Management background --3, modify classification
Aardio - 利用customPlus库+plus构造一个多按钮组件
Chapter 3: detailed explanation of class loading process (class life cycle)
2022-07-04 the high-performance database engine stonedb of MySQL is compiled and run in centos7.9
中国1,4-环己烷二甲醇(CHDM)行业调研与投资决策报告(2022版)
What are the interface tests? What are the general test points?