The default value for the ReturnType<typeof setInterval> in both the browser and node environments

I have a question about setting the initial value for the intervalTimer in an Interval that I need to save. The type is

ReturnType<typeof setInterval>
.

  interface Data {
    intervalTimer: ReturnType<typeof setInterval>
}

var data : Data = {
    intervalTimer: "initial value????",
}

function start(){
    data.intervalTimer = setInterval(()=>console.log('PING'), 3000)
}

I am looking for a solution that works on both Node and Browser, since they return different values. Setting intervalTimer to 0 results in an error

Type 'Timeout' is not assignable to type 'number'
.

Answer №1

While there may be a more sophisticated approach, one possibility is to establish an initial timeout that performs a no-operation task to ensure that the initial value of intervalTimer is of the correct type regardless of the environment:

interface Data {
    intervalTimer: ReturnType<typeof setTimeout>
}
const data: Data = {
    intervalTimer: setTimeout(() => void 0),
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering issues with @typescript-eslint/typescript-estree due to using a non-officially supported version of TypeScript after updating Nuxt

After upgrading Nuxt in my project using the command npx nuxi upgrade, I encountered an issue while running eslint .. The output displayed a warning regarding the TypeScript version: ============= WARNING: You are currently running a version of TypeScript ...

Is there a way to detect the presence of a named pipe in Node

When attempting to use named pipes in my application, I face an issue. If I try to connect to the named pipe before the server is running, an error occurs: events.js:141 throw er; // Unhandled 'error' event ^ Error: connect ENOENT & ...

Ways to differentiate the given hostname/URL as private in a NodeJS environment

I need assistance finding a method or NPM package to verify for private/local/bad addresses in the hostname entered as an input to my REST endpoint before saving it in the database. This measure is important to prevent SSRF attacks. Currently, I am only us ...

Can you explain the execution process of this Http.post method and provide details about the code path it follows

As I delve into the world of web development, one aspect that has me stumped is the functionality of the Http.post section within a project I stumbled upon on GitHub. Specifically, this pertains to an ExpressJS with Typescript repository I came across. So, ...

What is the maximum depth allowed in Vue?

My Vue component has the following structure: <transition name="fade"> <div> <div v-if="false"> </div> <div v-else=""> <div> <div> <div>no matter what content</div> ...

Enhancing collaboration: Seamlessly sharing interface/interface/model files in the integration of

Currently, I am engrossed in developing an application with an Express backend and Typescript whilst utilizing Angular for the frontend. The only snag I'm facing is that I require interface/models files from the backend to be accessible on the fronten ...

Leveraging Inversify for implementing a Multiinject functionality for a class with both constant and injected parameters

In my Typescript code, I have an insuranceController class that takes a multi inject parameter: @injectable() export class InsuranceController implements IInsuranceController { private policies: IInsurancePolicy[]; constructor(@multiInject("IInsu ...

Implementing ESM in your next.config.js file is critical for optimizing

Currently, I am in the process of optimizing a Next.js project and came across the requirement to include type: 'module' in thepackage.json file. However, this led to an error being thrown: Error [ERR_REQUIRE_ESM]: Must use import to load ES Mo ...

NodeJS - Issue: The procedure specified could not be located in the bcrypt_lib.node

After upgrading from Node.js 0.12.7 to 4.2.1, I encountered an error when attempting to start my server: $ node server.js C:\Users\me\documents\github\angular-express-auth\node_modules\bcrypt\node_modules\bindi ...

Challenges with Filtering in MongoDB

I can't seem to figure out what's going wrong with my code. I've checked the documentation and it all appears to be correct. I've also gone through several posts and attempted various solutions. Code: router.get('/rank/:rank' ...

Having trouble with the POST method in my Node.js application, and I'm unsure of the cause

This is the main index.js file that serves as the entry point for my Node.js application. const express = require('express'); const app = express(); const path = require('path'); const port = process.env.PORT || 2000; require('./db ...

Mastering Redux Toolkit - Ensuring Payload Type Verification is in Place when Invoking an Action Creator

I have been utilizing the redux toolkit for a while now and I appreciate how it helps in reducing redundant code. I am also interested in incorporating typescript, but I am facing challenges with getting the typechecking of an action payload to function pr ...

The filter() function seems to be failing to produce any results even though the callbackFn is functioning properly

I've created a function to filter certain JSON elements in an array using Vue 3. Here is the data structure: [ { "UID_Person": "160a5b9e-c352-39e3-802b-9cfcc126da77", "FirstName": "Maxi", ...

The Order ID field in the Serenity-Platform's Order Details tab is not registering orders

I've been working on replicating the functionality of Orders-Order detail in my own project. https://i.stack.imgur.com/Bt47B.png My custom module is called Contract and Contract Line item, which I'm using to achieve this. https://i.stack.imgur ...

Can you determine if the node.js code provided below is blocking or non-blocking?

I have the node.js script running on a server and want to determine if it is causing blocking issues. Here's a sample scenario: function addNewUser(name, callback) { userAccounts.findOne({name:name}, function(err, obj) { if (obj) { ...

"I am interested in using the MongoDB database with Mongoose in a Node.js application to incorporate the

I am facing a situation where I need to validate the name and code of a company, and if either one matches an existing record in the database, it should notify that it already exists. Additionally, when receiving data with isDeleted set to true, I want to ...

Retrieve the HTML content of all children except for a specific child element in jQuery

Is there a way to utilize jQuery/Javascript for selecting the HTML content of the two <p> elements in the initial <div class="description? I'm open to using Regex as well. This specific jQuery selection is being executed within Node.js on a c ...

Establishing parameters in a Socket.io chatroom

I am encountering an issue when attempting to store information in the socket room variables. The error message I receive is: UnhandledPromiseRejectionWarning: TypeError: Cannot set property 'host' of undefined This is the snippet of my code: io ...

Connect the v-model to a non-existent property (Array) in Vue JS

I am struggling with retrieving questions from the database and displaying them along with options on a webpage. Here is an example: <div v-for="(question,index) in questions"> <div class="interview__item-text ...

What is causing error code 401 to appear when trying to retrieve user items through a query?

When using the StrongLoop API Explorer, I am encountering an issue while trying to query: /People/{id}/food_prefs This query is supposed to return a JSON list of all the food preferences for a Person (User), where the Person is based on the built-in Us ...