---
title: "Discussion on \"Use Typescript generics for a type safe `setTimeout` and `setInterval`\" | Hashnode"
description: "Discussion on \"Use Typescript generics for a type safe `setTimeout` and `setInterval`\". TLDR;\nHere's the code:\ntype Handler = ((...args: any[]) => any) | string;\n\nfunction safeSetTimeout<F extends Handler>(\n    handler: F,\n    timeout?: number,\n    ...args: F extends string ? any[] : Parameters<F extends string ? never : F>\n) {\n    retu..."
url: "https://hashnode.com/posts/use-typescript-generics-for-a-type-safe-settimeout-and-setinterval-1/623b018fe99839749d247905"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Use Typescript generics for a type safe \`setTimeout\` and \`setInterval\`](https://abdellahcodes.com/use-typescript-generics-for-a-type-safe-settimeout-and-setinterval-1?utm_source=hashnode&utm_medium=feed)

TLDR; Here's the code: type Handler = ((...args: any\[\]) => any) | string; function safeSetTimeout<F extends Handler>( handler: F, timeout?: number, ...args: F extends string ? any\[\] : Parameters<F extends string ? never : F> ) { retu...

Mar 23, 2022 · 5 min read · abdellahcodes.com  
Read the article

-   [#typescript](https://hashnode.com/tag/typescript)
-   [#javascript](https://hashnode.com/tag/javascript)
-   [#reactjs](https://hashnode.com/tag/reactjs)
-   [#nodejs](https://hashnode.com/tag/nodejs)

## Written by

[abdellah hariti](https://hashnode.com/@a-hariti)

@a-hariti · Casablanca

  
[View profile](https://hashnode.com/@a-hariti)

I am a full stack web developer, with a focus on frontend. My main stack is Elm, Typescript, React, Svelte, and ofc node.js.

## Responses

Markdown supported · ⌘↵ to post

No responses yet.
