Tips for avoiding the error message "Expected 1 arguments, but got 0" when the specified argument is actually `undefined`

Current Typescript Version: 2.6.2

I am in the process of enhancing the type safety of redux beyond what is provided by default typedefs, while also streamlining some of the redundant code. I believe I am edging closer to my desired setup, with just one issue remaining. This is the current state of my implementation:

// Definition for Action based on redux typedefs
interface Action {
  type: any;
}

// Customized Action type using generics
// T is expected to be a string literal
// P is optional and defaults to undefined
interface TypedAction<T extends string, P = undefined> extends Action {
    readonly type: T;
    readonly payload: P;
}

// Function that generates common action creators
// Takes a TypedAction as its generic argument to infer
// the required payload in the returned action creator
function makeActionCreator<T extends TypedAction<any, any>>(
    type: T['type'],
): (p: T['payload']) => T {
    return payload => ({ type, payload } as T);
}

//=================
// Example usage //
//=================

interface EmailPasswordCredential {
    readonly email: string;
    readonly password: string;
}

enum SignUpActionType {
    executeSignUp = 'SignUp/executeSignUp',
    reportSuccess = 'SignUp/reportSuccess',
    reportError = 'SignUp/reportError',
}

////
// Action Types
////

// Sign-up action
// requires an email and password to be specified
type ExecuteSignUpAction = TypedAction<
    SignUpActionType.executeSignUp,
    EmailPasswordCredential
>;

// Success action
// Does not require a payload
type ReportSignUpSuccessAction = TypedAction<SignUpActionType.reportSuccess>;

// Error action
// Triggered when something fails (e.g., a 400 error)
// The error is passed back as the payload
type ReportSignUpErrorAction = TypedAction<SignUpActionType.reportError, Error>;

////
// Action Creators
////

const signUp = makeActionCreator<ExecuteSignUpAction>(
    SignUpActionType.executeSignUp,
);

const reportSignUpSuccess = makeActionCreator<ReportSignUpSuccessAction>(
    SignUpActionType.reportSuccess,
);

const reportSignUpError = makeActionCreator<ReportSignUpErrorAction>(
    SignUpActionType.reportSuccess, // this correctly causes an error due to type mismatch
);

// the real problem:

// Correctly throws an error as EmailPasswordCredential is required
signUp(); 

// should ideally work without any arguments
reportSignUpSuccess();

// Resolves the type error but seems unnecessary
reportSignUpSuccess(undefined);

For a detailed example with syntax highlighting and inline errors, click here.

Is there a solution to resolve the "Expected 1 arguments, but got 0" issue? Is the setup of TypedAction strange by defining undefined as the default?

Answer №1

Instead of using undefined, try using the void keyword.

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 an XHR error when using a systemjs module in TypeScript

Error: GET http://localhost:63342/Dog.js 404 (Not Found) XHR error (404 Not Found) loading http://localhost:63342/Dog.js <br/><br/>Below is the script in my index.html file. ...

Implementing onClick functionality to change background color with styled components

Is there a way to apply background color when a user clicks on any page in pagination using React styled components? I was able to achieve this with simple CSS by adding the class ".selected" which had a background-color of red, but now I need to use React ...

Issue: The formGroup function requires a valid FormGroup instance to be passed in. Data retrieval unsuccessful

My goal is to retrieve user data from a user method service in order to enable users to update their personal information, but I'm encountering an error. Currently, I can only access the "prenom" field, even though all the data is available as seen in ...

I am sorry, but it seems like there is an issue with the definition of global in

I have a requirement to transform an XML String into JSON in order to retrieve user details. The approach I am taking involves utilizing the xml2js library. Here is my TypeScript code: typescript.ts sendXML(){ console.log("Inside sendXML method") ...

Enter data into the appropriate columns

Within my Angular 6 application, I am creating a table with the following structure: Html: <table> <thead> <tr> <th> Property Details &nbsp; &nbsp; &nbsp; &nbsp; ...

Utilize JSX attributes across various HTML elements

I'm looking for a solution to efficiently add JSX attributes to multiple elements. Here are the example attributes I want to include: class?: string; id?: string; style?: string; And here are the example elements: namespace JSX { interface Int ...

I encountered TS2345 error: The argument type X cannot be assigned to the parameter type Y

Currently, I am delving into the world of Angular 8 as a beginner with this framework. In my attempt to design a new user interface with additional elements, I encountered an unexpected linting error after smoothly adding the first two fields. The error m ...

Implementing ETag in Angular 2

Implementing the odata standard which utilizes ETag has presented a challenge for me, particularly with PATCH requests. Each PATCH request requires sending the ETag in the header as If-None-Match. A HTTP status of 200 indicates that the change was successf ...

Transform the MUI Typescript Autocomplete component to output singular values of a specific property rather than a complete object

When utilizing MUI Autocomplete, the generic value specified in onChange / value is determined by the interface of the object set in the options property. For instance, consider an autocomplete with the following setup: <Autocomplete options={top ...

Error: Attempting to initiate a backward navigation action while already in the process. Utilizing Natiescript

I encountered an issue with the routing code in my Nativescript app. Here is the code snippet: const routes: Routes = [ { path: 'home', component: HomeComponent, canActivate: [AuthGuard], children: [ {path: 'fp&apos ...

What is the process for designing a type that accepts an object and provides an updated version?

Is there a way to create a function that can take an object and return a modified version of that object in this format? const data = { email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e08a8f888ea0848f85ce838f8d"& ...

Navigating through an interface array using *ngFor in TypeScript

After successfully implementing an interface to retrieve data from a service class, I encountered an issue when attempting to iterate through the FilteredSubject interface array. Despite using console.log, I was unable to achieve the desired outcome. You ...

What causes the difference in inference for unspecified generics between a simple function call and a null-coalescing operator in TypeScript?

What causes the different types of a and b in the given code snippet? function empty<T>() { return [] as T[] } const defEmpty = empty() function test1(abc: number[]|null) { const a = abc ?? defEmpty const b = abc ?? empty() } Upon testing on t ...

Consecutive requests to APIs using RxJs

Is it possible to efficiently make sequential API calls using RxJs? The challenge lies in the fact that the first Observable emits an array, and for each item in this array, a custom URL should be set for the next call. Additionally, certain conditions nee ...

The storage does not reflect updates when using redux-persist with next-redux-wrapper in a Typescript project

I'm currently working on integrating redux-persist with next.js using next-redux-wrapper. However, I'm facing an issue where the storage is not updating and the state is lost during page refresh. Below is my store.ts file: import { createStore, ...

Retrieve data from REST call to populate Material dropdown menu

I am looking to dynamically populate a dropdown menu with data retrieved from a Rest API. I attempted the following code: <Select id="country-helper"> {array.map((element) => ( <MenuItem value={element.code}>{element.country}& ...

Error in Svelte/Typescript: Encounter of an "unexpected token" while declaring a type

Having a Svelte application with TypeScript enabled, I encountered an issue while trying to run it: [!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript) src\api.ts (4:7) 2: 3: export default class API { 4: ...

In React, the issue arises when a Typescript declaration merging cause is referenced as a value but is being mistakenly used as a type

I am trying to come up with a solution to avoid the hassle of brainstorming names that seamlessly incorporate suffixes or prefixes in order to differentiate between declarations and component names. After being inspired by this resource Avoid duplicate id ...

Developing various VueJS TypeScript projects utilizing a shared library

In the process of developing two VueJS applications using TypeScript, I have created one for public use and another as an admin tool exclusively for my own use. Both applications are being built and tested using vue-cli with a simple npm run serve command. ...

Converting image bytes to base64 in React Native: A step-by-step guide

When requesting the product image from the backend, I want to show it to the user. The issue is: the API response contains a PNG image if the product has an image, but returns a (204 NO Content) if the product does not have an image. So, I need to display ...