简体   繁体   English

我如何在我的 auth.tsx 文件中使用 typescript 文件

[英]How can i use typescript file on my auth.tsx file

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.我有一个表单结构,这个表单在后端使用 jwt 令牌。 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.自然,我必须向这个 API 发出请求,所以我在操作文件下创建了一个名为 auth.tsx 的文件并编写了必要的代码,但是由于我的系统上有打字稿,我在某些数据类型中遇到了任何错误。

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谢谢

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 例如,看看这个游乐场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM