Home > Software design >  How can i use typescript file on my auth.tsx file
How can i use typescript file on my auth.tsx file

Time:10-31

My question will be a little easy, but I'm new to typescript so I'm having trouble coding it.

I have a form structure and this form works with jwt tokens in the backend. Naturally, I have to make requests to this API, so I created a file named auth.tsx under an action file and wrote the necessary codes, but since I have typescript on my system, I am getting any errors in some data types.

Here are the data types I'm getting error on and an example of a variable

export const reset_password_confirm = (uid, token, new_password, re_new_password) => async dispatch => {
    const config = {
        headers: {
            'Content-Type': 'application/json'
        }
    };

    const body = JSON.stringify({ uid, token, new_password, re_new_password });

    try {
        await axios.post(`${process.env.REACT_APP_API_URL}/auth/users/reset_password_confirm/`, body, config);

        dispatch({
            type: PASSWORD_RESET_CONFIRM_SUCCESS
        });
    } catch (err) {
        dispatch({
            type: PASSWORD_RESET_CONFIRM_FAIL
        });
    }
};

and then i get this error:

The 'new_password' parameter has an implicit type 'any'.

I also get this error in the variables I listed below.

uid, token, email, new_password, password, dispatch. 

How can i use typescript in this file Can anyone help please. Thanks

CodePudding user response:

In typescript function arguments need types. This enforces what kind of thing can be passed to a function, and lets you know how to use that thing inside the function.

For example:

import { DispatchOrSomething } from 'redux-or-whatever-lib'

export const reset_password_confirm = (
  uid: number,
  token: string,
  new_password: string,
  re_new_password: string
) => async (dispatch: DispatchOrSomething) => {
  //...
}

For example, look at this playground

  • Related