Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Typescript扩展HTTP响应接口_Node.js_Typescript - Fatal编程技术网

Node.js Typescript扩展HTTP响应接口

Node.js Typescript扩展HTTP响应接口,node.js,typescript,Node.js,Typescript,因此,我使用axios库编写了以下代码: const BTrustURLResponse: Response = await axios.get(`${process.env.BTRUST_URL}/flow/${process.env.BTRUST_FLOWID}/link?callback_url=${callback_url}`, { headers: { 'Authorization': `Bearer ${process.env.BTRUST_API_KEY}`

因此,我使用
axios
库编写了以下代码:

const BTrustURLResponse: Response = await axios.get(`${process.env.BTRUST_URL}/flow/${process.env.BTRUST_FLOWID}/link?callback_url=${callback_url}`, {
    headers: {
        'Authorization': `Bearer ${process.env.BTRUST_API_KEY}`,
    },
});
我确信(
console.log(Object.keys(BTrustURLResponse))
)返回的对象具有属性
数据
。但是默认情况下,
Response
接口不包括
数据
属性

我怎样才能修好它

我尝试了以下方法:

  • 创建了
    @types
    目录,带有
    响应
    目录,然后:
  • 这就是文件本身:

     declare global {
        export interface Response {
            data?: string,
        }
    }
    
  • 然后我在
    tsconfig.json
    中做了以下操作:
    类型根“:[“@types”,“/node\u modules/@types”]
  • 但我仍然无法使用
    .data
    响应

    从文档中看,似乎
    数据:{}
    是一个对象,但您使用字符串作为数据对象的类型<代码>数据?:字符串
    ,这可能是个问题。
    如果您不知道响应的结构,那么首先可以使用
    数据?:any
    ,如果这样做有效,那么您可以提供适当类型的
    数据对象。

    您不必创建单独的@types文件来创建
    响应
    接口。Axios有一个名为
    AxiosResponse
    的通用接口,它从
    get(url:string,config?:AxiosRequestConfig):Promise获取
    T
    通用变量。。然后,
    T
    generic var用于
    AxiosResponse的
    data
    属性

    见此:

    interface ResponseData{
      _id?: string
      //... or any "property" you wanna add
    }
    
    const BTrustURLResponse = await axios.get<RespnseData>(`${process.env.BTRUST_URL}/flow/${process.env.BTRUST_FLOWID}/link?callback_url=${callback_url}`, {
        headers: {
            'Authorization': `Bearer ${process.env.BTRUST_API_KEY}`,
        },
    });
    
    接口响应数据{
    _id?:字符串
    //…或任何你想添加的“财产”
    }
    const BTrustURLResponse=wait axios.get(`process.env.BTRUST\u URL}/flow/${process.env.BTRUST\u FLOWID}/link?callback\u URL=${callback\u URL}`{
    标题:{
    'Authorization':`Bearer${process.env.BTRUST\u API\u KEY}`,
    },
    });
    

    希望它能起作用……

    这能回答你的问题吗?