Let's say there's a function with parameters paramA (string) and paramB (object)
const dummyFunction = (paramA: string, paramB: { defaultValue: any }) => {
}
I want to remove the any from the defaultValue key in paramB object (It can be of any type)
Requirement is to give type to function return value based on defaultValue key in paramB . How to achieve it?
The thing which you need is generics .
Not sure if I got your intention right, but you would need to go with something like this:
const dummyFunction = <T>(paramA: string, paramB: { defaultValue: T }): T => {
}
It will then infer the return type from the type of the defaultValue
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.