TypeScript Return Type For SetTimeout Function
TypeScript
23/09/2021
Without actually knowing the return type of setTimeout
, we can have TypeScript infer the type by using the utility type ReturnType<Type>
.
TYPESCRIPT
const timer: ReturnType<typeof setTimeout> = setTimeout(() => { console.log('Hello World!') }, 1000)
If you then hover over the timer
constant, you'll see that it's of type number
. In fact, setTimeout
returns a numeric id.