Exploring the Use of Overloads and Callbacks in TypeScript

Currently, I am exploring the implementation of the builder pattern and trying to create functions that support optional parameters as arguments. Here is an example of how I am approaching this:

new Service().OnAction(Actions.Add, () => { alert(1); })
    .OnAction(Actions.Subtract, () => { alert(1); })
    .ServiceBuilder(serviceOptions);

new Service().OnAction(Actions.Add, (vm, container) => { 
        vm.FirstName = container.find("data-id=FirstName").val();
        vm.LastName = container.find("data-id=LastName").val();
    })
    .OnAction(Actions.Subtract, (vm, container) => { alert(1); })
    .ServiceBuilder(serviceOptions);

The code above represents some of my attempts in incorporating this feature.

public OnAction(actions: Actions, x: (y?:any, z?:any) => void) {
    if (y != undefined && z != undefined) {
        x(y, z);
    }
    else
        x();

    return this;
}

I have also referred to the following documentation for additional insights: https://www.typescriptlang.org/docs/handbook/functions.html

Answer №1

To enable optional parameters for the method OnAction, you must move the logic that checks for undefined values to the method being passed to x.

Here is an example:

public OnAction(actions: Actions, x: (y?:any, z?:any) => void) {
    x(y, z);
    return this;
}

new Service().OnAction(Actions.Add, (vm, container) => {
    if (vm === undefined && container === undefined) {
        // handle logic for undefined values
        return;
    }

    vm.FirstName = container.find("data-id=FirstName").val();
    vm.LastName = container.find("data-id=LastName").val();
})

In your scenario, it doesn't matter how you call the method x:

x();

or:

x(y, z);
If both <code>y and z are undefined, it has the same effect.

Answer №2

During runtime, it becomes difficult to distinguish between function signatures. The Function object has a length property that can indicate the number of parameters in a function. This can be used to differentiate functions based on the number of arguments they accept, although there is a known issue with the length property and optional parameters when transpiling to ES5.

However, the key point here is that it shouldn't matter whether a function actually accepts the parameters or not. You can simply pass all the supported parameters, and if the function uses them, great; if not, no harm is done:

class MyClass {
    
    public OnAction(actions: Actions, x: (y?:any, z?:any) => void) {
        // Here we pass both arguments
        // for demo purposes, argument values are set to null - replace with actual parameters 
        x(null, null); 
     }
}

new MyClass().OnAction(Actions.add, ()=> {}) // Passing arguments that go unused
new MyClass().OnAction(Actions.add, (vm)=> { console.log(vm) }) // One argument being utilized
new MyClass().OnAction(Actions.add, (vm, c)=> { console.log(vm);  console.log(c)}) // Both arguments being utilized

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

The category has been defined but remains inaccessible. What could be the reason for this limitation?

I've been utilizing this bson library along with the corresponding declaration found here. Within this library, there is a method called serialize():Buffer which returns a Buffer. When I execute the following code: let data:Buffer = this.serializer.s ...

The module './product' could not be located, resulting in error TS2307

app/product-detail.component.ts(2,22): error TS2307: Cannot find module './product'. I have tried several solutions but none of them seem to work for me. I am working on a demo app in Angular 2 and encountering this specific error. Any guidance ...

The RxJs 'from' function is currently producing an Observable that is unrecognized

import { Tenant } from './tenant'; import { from, Observable } from 'rxjs'; export const testTenants: Tenant[] = [ { 'tenant_id': 'ID1' } ] const tenants$: Observable<Tenant>= from(testTenant ...

Type A can be assigned to the limitation of type T, although T may be instantiated with a varying subtype constraint of either A or B

I keep receiving this confusing error from TypeScript. My T generic should be fully compatible with types A | B since it extends from it! The error is incorrect in saying that you can't instantiate it with an incompatible type. type MyProps<T exten ...

What is the proper way to define a Map in TypeScript?

In my TypeScript project, I'm trying to define a simple map structure with string keys and an object as a value. After researching on StackOverflow, this is the code snippet I've put together: class Person { id: number; name: string; } ...

In Angular, when using multiple-selection mode in mat selection, the Error Value should always be in the form of

**Everything is working fine except for one error in the console. How can I remove this error? Can anyone please help me? Save, Edit, and searching are working perfectly fine. ** public campaignCategoryFormGroup$: FormGroup = this.fb.group({ // 'c ...

Issue: Only one type can be named "Upload" within Apollo, Express, and Type-Graphql

I've encountered an issue while trying to execute a simple Mutation for uploading an image. The error I keep facing is: "Error: There can be only one type named 'Upload'." Here's the snippet of my code: import { FileUploadI, GraphQLUp ...

"Implement a specific background-color for a component in Angular 4 by adding it to the body tag

My project is built on angular 4, and I am looking to change the background color of the <body> tag using the component's .ts file when a specific element is clicked. The issue I'm facing is that once the color is changed to blue, it persi ...

Incorporate a service into a base class in Angular2 to ensure its functionality extends to all derived classes

I have multiple classes with a hierarchical inheritance structure as follows: class A (an abstract class) class B extends A class C extends B I am looking to incorporate a service into class A to enable a function that utilizes notifications. How can I ...

Injecting dynamic templates in Angular 7

Let me simplify my issue: I am currently using NgxDatatable to display a CRUD table. I have a base component named CrudComponent, which manages all CRUD operations. This component was designed to be extended for all basic entities. The challenge I am en ...

Most effective methods for validating API data

Currently, I am working on developing an api using nestjs. However, I am facing some confusion when it comes to data validation due to the plethora of options available. For instance, should I validate data at the route level using schema validation (like ...

encountering a problem integrating react-dropzone into a project using react-16.13.1

I'm having an issue adding the package https://www.npmjs.com/package/react-dropzone to my TypeScript React project using Yarn as the package manager. I ran the command yarn add @types/react-dropzone, but it ended up installing a deprecated package h ...

The React.FC component encountered an invalid hook call

I've encountered an issue while using TypeScript and trying to implement a hook within React.FC. Surprisingly, I received an error message stating that hooks can only be used inside functional components. But isn't React.FC considered a functiona ...

Tips for effectively transmitting data while utilizing a declarative/reactive data access method with RxJS in Angular?

Angular typically follows a classic pattern for data access that looks like this: Traditional Data Access Pattern getProducts(): Observable<Product[]> { return this.http.get<Product[]>(this.productsUrl) .pipe( tap(data => consol ...

Display a message on React when hovering over

Is there a way to display a message when the user hovers over the 'Validate' button while it is disabled? I attempted the code below but it did not work as expected. <button type="button" className="frm_btn" ...

Enhance ReactRouter by incorporating types into the NavLink isActive() function

Currently immersed in a React project powered by TypeScript and utilizing ReactRouter. There's a particular section where the NavLink hook is employed like so: const {itemPath} = props // itemPath represents a string return ( <Router> &l ...

Is there a way to include values in the body of an HTTP GET request using Angular?

I've created a function in my service that looks like this: /** * Retrieve all data * @param sendSelectedValues string */ getAllActPlanBalanceYearData(sendSelectedValues: any): Observable<any> { const url = `/yearlyvalues/act-and ...

Converting Typescript to Javascript: How to export a default object

Perhaps this question has been addressed before in some manner, however, I was unsure of how to phrase it. In my Typescript file, there is a single class being exported: export class MyClass { ... } In another Javascript file, I import the transpile ...

Empty screen on the RadCartesianChart

I've recently started learning about Nativescript development and encountered an issue while trying to implement the Nativescript-ui-chart in my app. When I load the page I created, the screen goes blank with nothing being displayed. I followed the i ...

What is the process for implementing token authentication within headers using an interceptor, especially when the token may be null?

In my Angular13 application with OAuth authentication, I am encountering issues when trying to add the token for all services. I have been unsuccessful in managing the undefined token. Despite trying various techniques to retrieve the token, I always enco ...