简体   繁体   English

我在 .tsx 文件中反应打字稿代码有问题

[英]I've problem with react typescript code in .tsx file

I have a problem with my typescript usage.我的打字稿使用有问题。

First, let me tell you what I want to do:首先,让我告诉你我想做什么:

First of all, I have an advanced user login system that manages user logins in the backend, so I need to include many file systems in order to integrate it into the react side.首先,我有一个高级的用户登录系统,在后台管理用户登录,所以我需要包含很多文件系统,以便将它集成到react端。 One of them is the auth.tsx其中之一是auth.tsx

file located under the actions file.文件位于操作文件下。 The purpose of this file is to make the necessary API calls to the server and transfer the information there to the form design, so I will have a much neater code structure.这个文件的目的是对服务器进行必要的 API 调用并将那里的信息传输到表单设计中,所以我会有一个更整洁的代码结构。 But unfortunately, because there is typescript in the system, I get some errors and I can't figure out why.但不幸的是,因为系统中有打字稿,我得到了一些错误,我无法弄清楚为什么。 Can you help?你能帮我吗?

this is piece of code from auth.tsx file这是来自auth.tsx文件的一段代码

export const facebookAuthenticate = (state:string, code:string) => async dispatch => {
    if (state && code && !localStorage.getItem('access')) {
        const config = {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        };

        const details = {
            'state': state,
            'code': code
        };

        const formBody = Object.keys(details).map(key => encodeURIComponent(key) + '=' + encodeURIComponent(details[key])).join('&');

        try {
            const res = await axios.post(`${process.env.REACT_APP_API_URL}/auth/o/facebook/?${formBody}`, config);

            dispatch({
                type: FACEBOOK_AUTH_SUCCESS,
                payload: res.data
            });

            dispatch(load_user());
        } catch (err) {
            dispatch({
                type: FACEBOOK_AUTH_FAIL
            });
        }
    }
};

In the dispatch parameter on this code, the dispatch parameter has an implicit type 'any' .在此代码the dispatch parameter has an implicit type 'any'the dispatch parameter has an implicit type 'any' I get the error also in encodeURIComponent(details[key])) ,我也在encodeURIComponent(details[key]))收到错误,

the expression of type 'string' is '{ state: string; code: string; Element has type 'any' implicitly because it cannot be used to index type '}'.
  '{ state: string; code: string; I am getting the error ts(7053) not found directory signature with parameter of type 'string' on type '}'.

Please help me请帮我

The TS type of return value for Object.keys() is string[] , see below TS type definition: Object.keys()的返回值的 TS 类型是string[] ,参见下面的 TS 类型定义:

lib.es5.d.ts : lib.es5.d.ts

keys(o: object): string[];

But the type of the key should be 'state' | 'code'但是key的类型应该是'state' | 'code' 'state' | 'code' rather than any string. 'state' | 'code'而不是任何字符串。 So you should Type Assertions to specify a more specific type.所以你应该Type Assertions来指定一个更具体的类型。

const details = {
    'state': state,
    'code': code
};

const formBody = (Object.keys(details) as (keyof typeof details)[]).map(key => encodeURIComponent(key) + '=' + encodeURIComponent(details[key])).join('&');

TypeScript Playground 打字稿游乐场

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

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