const isNumber = ( val ) : val is number => typeof val === 'number' ; export interface Interval { remove : ( ) => void ; start : ( _ms ? : number | undefined ) => NodeJS . Timeout | undefined ; } export function useInterval ( callback , ms ? ) { let intervalId = undefined ; const remove = ( ) => { if ( ! intervalId ) return ; clearInterval ( intervalId ) ; intervalId = undefined ; } ; const start = ( _ms ? ) => { remove ( ) ; if ( ! _ms && ! ms ) { return ; } const m = ( _ms || ms ) as number ; return ( intervalId = setInterval ( callback , m ) ) ; } ; if ( isNumber ( ms ) ) { start ( ) ; } return { remove , start } ; }