I'm struggling with return type
for the current function (example):
const someFunction = () => {
if (condition) {
return Promise.resolve()
}
return new Promise(resolve => setTimeout(resolve, 1000))
}
How do I define a type
that this function returns?
I know that I can set unknown
or any
, but I'm looking for a cleaner approach.
You can use Promise
as generic,
const someFunction = (): Promise<void> => {
if (condition) {
return Promise.resolve()
}
return new Promise(resolve => setTimeout(resolve, 1000))
}
instead of void
you can use another type, based on your Promise resolve
I think, Promise<any>
is a good solution here. Since, we haven't defined return
type from resolve
methods.
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.