Using an Interface in a Callback Function with Typescript

Recently, I have started using Typescript and there's something I am uncertain about.

I am utilizing an npm package called azure-storage and specifically invoking a method called doesBlobExist:

blobService.doesBlobExist(containerName, blobName, (callbackResult: ErrorOrResult<BlobService.BlobResult>) => {
        // Want callbackResult.response here
});

The doesBlobExist function (which comes from the npm package) looks like this:

doesBlobExist(container: string, blob: string, callback: ErrorOrResult<BlobService.BlobResult>): void;

Additionally, the ErrorOrResult type is an interface with the following structure:

interface ErrorOrResult<TResult> {
    (error: Error, result: TResult, response: ServiceResponse): void
}

My uncertainty lies in the fact that when I call the function, I thought I could utilize the interface like this:

callbackResult.response since it is defined in the interface. However, I keep getting null when accessing callbackResult. Upon investigating, I found that it is being set as the error: Error from the interface.

Therefore, I am wondering if it is feasible to achieve what I desire above, or if I need to use the function in this manner:

blobService.doesBlobExist(containerName, blobName, (error, result, response) => {
        // specify the 3 items directly in the interface
});

Answer №1

section, here's a unique rewrite of the text:

After reviewing your issue, you have the option to implement the solution provided above or try something similar to this:

blobService.checkIfBlobExists(containerName, blobName, (error, result, response) => {
        if(error !== null){
           // Handle the error case
           return;
        } 
        // Process the response data here
});

If you prefer to skip the error check, you can directly work with the response. However, make sure to consider all possible response scenarios.

Although callbackResult.response is specified in the interface, callbackResult continues to return as null. Upon investigation, it appears to be set as the error: Error from the interface.

It seems that you are receiving a null value because the functions are not yielding any errors.

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

Extracting data from a JSON object using Angular 2

I need advice on the most efficient way to handle JSON within my angular2 application. The JSON data I am working with includes: { "rightUpperLogoId": { "id": 100000, "value": "" }, "navbarBackgroundColorIdCss": { "id" ...

The Tanstack react-table feature is limited in its ability to output tsx from the cell

Currently conducting a test on Tanstack react-table library using React and TypeScript. It appears that I am encountering an issue with returning tsx/jsx from the cell function of ColumnDef: https://i.sstatic.net/d5X3y.png Is there something crucial that ...

Angular HTTP post is failing on the first attempt but succeeds on the second try

Just started working on my first Angular exercise and encountered an issue where I am receiving an undefined value on the first attempt from an HTTP post request. However, on the second try, I am getting the proper response in Edge and Firefox. Thank you f ...

Updating a property in a JavaScript object using Angular

Working with Angular, I have a dataset: export class AppComponent { data = [ { "area1": { "format": "changethis" } ] I am looking to develop a function that can alter the value of a specific key. For e ...

Breaking up and Substituting text within Angular 8's HTML structure

When I retrieve data from a REST api, I need to split the name parameter at '2330' and insert a line break. For example, if the name is: ABCD 2330 This is My Name, I want the output on my screen to appear as: ABCD 2330 This is My Name // this par ...

Unable to exclude specific files using VSCode's "files.exclude" feature

In my workspace settings file, the configuration for excluding certain files is as follows: { "files.exclude": { "**/*.js": { "when": "$(basename).ts" }, "app/**/*.js.map": { "when": "$(basename).ts" ...

Show the subscription response data in Angular

When utilizing the code snippets below from two different components, I am able to receive a valid response value from the subscriber. dataService.ts fetchFormData(){ return this.http.get('http://localhost:48116/RecuruitmentService.asmx/addRoleTest ...

Using prevState in setState is not allowed by TypeScript

Currently, I am tackling the complexities of learning TypeScipt and have hit a roadblock where TS is preventing me from progressing further. To give some context, I have defined my interfaces as follows: export interface Test { id: number; date: Date; ...

Enhancing Angular 4 classes with dependency injection techniques

Currently utilizing angular 4 and angular cli for my project development. I have created some classes that serve as the base for my components! However, as the constructors of these classes grow during development, I find myself in a phase where I need to ...

The process of extracting a value from an array of objects encountered an error due to the undefined object

I am looking to extract the value from an array within an object while also implementing error checking. The code I currently have checks if a specific key exists in the object and if the value associated with that key is of type array. If both condition ...

Formulate a multi-line string using a collection in React's JavaScript framework

I'm working on a React function that involves a set and I need to update an HTML element using the data from this set. Below is an example of my code: const updateElement = (mySet) => { document.getElementById('myId').innerHTML = Arra ...

Invoking a self-executing anonymous function using requestAnimationFrame

Recently, I developed a basic 2D-tile-renderer using JavaScript and decided to convert it to TypeScript. The process went smoothly, with the only challenge being when I tried to call window.requestAnimationFrame with a callback function. Eventually, I was ...

How does the C# compiler manage interfaces inheriting multiple interfaces?

Recently, I made the discovery that C# has the capability for interfaces to inherit from one or more base interfaces. For example, the IScreen in Caliburn.Micro does this by extending namespace Caliburn.Micro { public interface IScreen : IHaveDis ...

Exporting the statement from Ionic 3 page.module.ts file

Recently, I started working on Ionic 3 and decided to implement the new page lazy loading approach. Specifically, I have a page named ControlPage that I am focusing on. In most of the resources I referred to, it was recommended to include the following co ...

Verify Angular Reactive Form by clicking the button

Currently, I have a form set up using the FormBuilder to create it. However, my main concern is ensuring that validation only occurs upon clicking the button. Here is an excerpt of the HTML code: <input id="user-name" name="userName" ...

Complete set of keys within a type

In my TypeScript project, I am working with an API (specifically OData API) to retrieve data. This API allows the selection of specific fields to retrieve. For example, api/some/getbyid(42)?$select=x,y,z can be used to get fields x, y, and z, along with s ...

Encountering difficulty when determining the total cost in the shopping cart

I am currently working on a basic shopping cart application and I am facing an issue when users add multiple quantities of the same product. The total is not being calculated correctly. Here is my current logic: Data structure for Products, product = { ...

The Angular error message InvalidValueError is thrown when the Map function expects a mapDiv of type HTMLElement, but instead receives a

When attempting to initialize Google Maps, I encountered a particular problem. In this div, I am trying to load the map but consistently receiving the same error message. I attempted to use ngAfterViewInit() in case the view wasn't fully loaded befo ...

What does `(keyof FormValues & string) | string` serve as a purpose for?

Hey there! I'm new to TypeScript and I'm a bit confused about the purpose of (keyof FormValues & string) | string. Can someone please explain it to me? export type FieldValues = Record<string, any>; export type FieldName<FormValues ...

Issue encountered while generating a dynamic listing using Angular

My goal is to generate a dynamic table using Angular. The idea is to create a function where the user inputs the number of rows and columns, and based on those values, a table will be created with the specified rows and columns. However, I am facing an iss ...