Ways to resolve TypeScript error(ts:2740) similar to this

Apologies, I require a translator for English. Please provide the code in your response. Thank you.


// [message]: Type 'Promise<unknown>' is missing the following properties from type 'IDBDatabase': name, objectStoreNames, onabort, onclose, and 10 more.ts(2740)

const /*[message] here("IDB") -->*/ IDB: IDBDatabase | null = (async () => {
    return (await new Promise((resolve, reject) => {
        const request = indexedDB.open("idb")
        request.onerror = (event) => {
            resolve(null)
        }
        request.onupgradeneeded = (event) => {
            resolve(null)
        }
        request.onsuccess = (event) => {
            resolve(request.result as IDBDatabase)
        }
        request.onblocked = () => {
            resolve(null)
        }
    }))
})()

Answer №1

You seem to have some async function misuse. The async function you are using returns a

Promise<IDBDatabase | null>
, meaning you cannot assign it directly to IDBDatabase | null

When the promise resolves, there are 2 ways you can execute your code:

  • Using await in an async function
  • Utilizing then on the promise for providing a callback
function getDBPromise(): Promise<IDBDatabase|null> {
  return new Promise((resolve, reject) => {
        const request = indexedDB.open("idb")
        request.onerror = (event) => {
            resolve(null)
        }
        request.onupgradeneeded = (event) => {
            resolve(null)
        }
        request.onsuccess = (event) => {
            resolve(request.result as IDBDatabase)
        }
        request.onblocked = () => {
            resolve(null)
        }
    })
}

async function awaitInAsyncFunction() {
    const IDB: IDBDatabase | null = await getDBPromise();
}


getDBPromise().then((db) => {
    const IDB: IDBDatabase | null = db;
})

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

Is there a way to properly test a Vue component that is watching a ref prop?

I am currently testing a Vue component that should display a success icon for 3 seconds when the loading state changes from true to false. I have encountered challenges while trying to write a component test using Vue Testing Library or Vue test utils du ...

Error: The next.config.js file contains invalid options - The root value includes an unexpected property

I recently updated my next version from 10 to 12, and when I run the local development server, I encounter the following error in the terminal. As a result, the code fails to compile. How can I fix this issue? Invalid next.config.js options have been iden ...

This TypeScript error indicates that the variable may be undefined (Error code: 18048)

One of the challenges I encountered in my Angular project was with an interface defined in userinterface.ts export interface Done { wordlen: number; word: string; }; I utilized this interface to populate an array like so donearr: Done[] = []; ...

Guide on updating a value in a Firestore document using Firebase

I am working on updating a specific portion of a document in my Firebase collections structure, specifically the phonebook(map) section. https://i.sstatic.net/UmHot.png When attempting to modify the document, I encountered an error saying Invalid documen ...

Is there a way to assign a function signature to every property within a class in typescript?

When I am structuring a class to contain all methods related to its functionality, I ensure that all the methods within the class have a specific signature. I attempted to utilize an index signature in this manner: interface rulesType { [key:string]: t ...

Encountering a 405 error when making an OpenAI API call with next.js, typescript, and tailwind CSS

I encountered a 405 error indicating that the method request is not allowed. I am attempting to trigger an API route call upon clicking a button, which then connects to the OpenAI API. Unsure of my mistake here, any guidance would be highly appreciated. E ...

Utilize an enum to serve as a blueprint for generating a fresh object?

I've defined an enum as shown below: export enum TableViewTypes { user = 'users', pitching = 'pitching', milestones = 'milestones', mediaList = 'mediaList', contacts = 'contacts' } ...

How can an Angular 4 HTML for loop display a loosely typed object (string) normally but behave differently when the element is extracted directly?

Currently, I am utilizing Angular 4 to create an application that primarily involves presenting data from a database and performing CRUD operations. In my experience with Angular 4, I have noticed that the component's HTML does not handle loosely-typ ...

Import statement cannot be used except within a module

I am currently facing an issue with running the production version of my code. I have Node 20.10 and TypeScript 5 installed, but for some reason, I am unable to run the built version. Here are the contents of my package.json and tsconfig.json files: { & ...

The database migration encounters an issue: The module 'typeorm' cannot be located

When I run the following commands: ❯ node --version v16.19.0 ❯ yarn --version 3.5.0 I am attempting to launch this project: https://github.com/felipebelinassi/typescript-graphql-boilerplate However, when I execute: yarn db:migrate which runs the c ...

Adjust the column count in mat-grid-list upon the initial loading of the component

My goal is to implement a mat-grid-list of images with a dynamic number of columns based on the screen size. Everything works perfectly except for one small glitch – when the grid first loads, it defaults to 3 columns regardless of the screen size until ...

Unable to remove a OneToMany entry in TypeORM

I am currently working with the following database schemas: @Entity() export class Question extends BaseEntity { @PrimaryColumn() messageId: string; @Column() authorId: string; @Column() question: string; @Column("varchar", { arr ...

Implementing ExpressJS functionality within a TypeScript class

Searching through numerous examples of ExpressJS and TypeScript, I noticed they all follow a similar pattern: import * as express from "express"; let app = express(); app.use("/", express.static("/")); However, my preference is to employ the class appro ...

Converting a JSON string into an ES6 map or another data structure in JavaScript in order to maintain the order of keys

Are there any built-in methods in ES6, JavaScript, or TypeScript to directly convert a JSON string to an ES6 map while preserving the order of keys, or is implementing a custom parser the only option? Note: I am intentionally avoiding the use of "parse" t ...

Clearing the Value of a Linked Ant Design Cascading Dropdown Select in Next.js/React.js

I'm currently developing a React form with the assistance of Ant Design's Form component. The form boasts various dropdowns such as facility, specialization, and doctor. It is imperative that when the values in the facility or specialization drop ...

In the en-US locale, the toLocaleDateString function is transforming "00:30" into 24:30

Questioning the Conversion of Time from 00:30 to 24:30 in en-US Locale options = { year: "numeric", day: "numeric", month: "numeric", hour: '2-digit', minute: '2-digit&apo ...

Tips for troubleshooting TypeScript Express application in Visual Studio Code

Recently, I attempted to troubleshoot the TypeScript Express App located at https://github.com/schul-cloud/node-notification-service/ using Visual Studio Code. Within the launch.json file, I included the following configuration: { "name": "notifi ...

What steps can I take to resend a fetch request in React.js if I receive a 408 or 503 error?

There are times when I am fetching data from a server and the request takes too long, resulting in the server responding with a 408 status code. This is how my code currently looks: export class ResponseError extends Error { public response: Response; ...

Exploring the world of Typescript and Angular Filter functionalities

I am looking to utilize one of my Angular Filters in my controller as a function. I came across a solution on this page: How to use a filter in a controler The last answer provided exactly what I needed, so I implemented it in my JS code: var MyFunc ...

Creating a JSON array or JSON object from an Angular Material table

My task is to create a JSON array or object from an Angular Material table, which I can then utilize to export to an Excel sheet. Here is the data: const ELEMENT_DATA: Element[] = [ {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: &apo ...