Typescript 为什么以下重写方法返回承诺<;HTTPResponse<;缓冲区>&燃气轮机;当responseType参数为’;t集? 从“http”导入{IncomingHttpHeaders} 键入Method=“POST”|“GET”|“PATCH”|“PUT”|“DELETE” 类型标题={ [键:字符串]:字符串 } 类型JSON={ [键:字符串]:任何 } 键入ResponseType=“buffer”|“json”|“text” 接口HTTPRequest{ 方法:方法 url:string 标题?:标题 qs?:{ [键:字符串]:字符串 } json?:json } 接口HTTPRequestOptions{ 标题?:标题 responseType?:responseType 是否重试?:{ 限制?:数量 方法?:方法[] } } 接口HTTPResponse{ 状态代码:编号 标题:Incomingttpheaders 正文:T 请求:HTTPRequest } 类HTTP{ 公职( url:HTTPRequest[“url”], json?:HTTPRequest[“json”], 选项?:HTTPRequestOptions&{responseType:“buffer”} ):承诺 公职( url:HTTPRequest[“url”], json?:HTTPRequest[“json”], 选项?:HTTPRequestOptions&{responseType:“json”} ):承诺 公职( url:HTTPRequest[“url”], json?:HTTPRequest[“json”], 选项?:HTTPRequestOptions&{responseType:“text”} ):承诺 公职( url:HTTPRequest[“url”], json?:HTTPRequest[“json”], 选项?:HTTPRequestOptions ):承诺 公职( url:HTTPRequest[“url”], json?:HTTPRequest[“json”], 选项?:HTTPRequestOptions ):承诺{ 把这个还给我( { 方法:“张贴”, url:url, json:json, }, 选择权 ) } } const httpInstance=new HTTP() ;(异步()=>{ //为什么'response1'是'HTTPResponse'类型? const response1=等待httpInstance.post( "https://api.example.net/hello" ) //以下行为符合预期(`response2`属于`HTTPResponse`类型) const response2=等待httpInstance.post( "https://api.example.net/hello", 无效的 { 响应类型:“json” } ) })()

Typescript 为什么以下重写方法返回承诺<;HTTPResponse<;缓冲区>&燃气轮机;当responseType参数为’;t集? 从“http”导入{IncomingHttpHeaders} 键入Method=“POST”|“GET”|“PATCH”|“PUT”|“DELETE” 类型标题={ [键:字符串]:字符串 } 类型JSON={ [键:字符串]:任何 } 键入ResponseType=“buffer”|“json”|“text” 接口HTTPRequest{ 方法:方法 url:string 标题?:标题 qs?:{ [键:字符串]:字符串 } json?:json } 接口HTTPRequestOptions{ 标题?:标题 responseType?:responseType 是否重试?:{ 限制?:数量 方法?:方法[] } } 接口HTTPResponse{ 状态代码:编号 标题:Incomingttpheaders 正文:T 请求:HTTPRequest } 类HTTP{ 公职( url:HTTPRequest[“url”], json?:HTTPRequest[“json”], 选项?:HTTPRequestOptions&{responseType:“buffer”} ):承诺 公职( url:HTTPRequest[“url”], json?:HTTPRequest[“json”], 选项?:HTTPRequestOptions&{responseType:“json”} ):承诺 公职( url:HTTPRequest[“url”], json?:HTTPRequest[“json”], 选项?:HTTPRequestOptions&{responseType:“text”} ):承诺 公职( url:HTTPRequest[“url”], json?:HTTPRequest[“json”], 选项?:HTTPRequestOptions ):承诺 公职( url:HTTPRequest[“url”], json?:HTTPRequest[“json”], 选项?:HTTPRequestOptions ):承诺{ 把这个还给我( { 方法:“张贴”, url:url, json:json, }, 选择权 ) } } const httpInstance=new HTTP() ;(异步()=>{ //为什么'response1'是'HTTPResponse'类型? const response1=等待httpInstance.post( "https://api.example.net/hello" ) //以下行为符合预期(`response2`属于`HTTPResponse`类型) const response2=等待httpInstance.post( "https://api.example.net/hello", 无效的 { 响应类型:“json” } ) })(),typescript,Typescript,找到了罪犯 必须删除重载中的? 从“http”导入{IncomingHttpHeaders} 键入Method=“POST”|“GET”|“PATCH”|“PUT”|“DELETE” 类型标题={ [键:字符串]:字符串 } 类型JSON={ [键:字符串]:任何 } 键入ResponseType=“buffer”|“json”|“text” 接口HTTPRequest{ 方法:方法 url:string 标题?:标题 qs?:{ [键:字符串]:字符串 } json?:json } 接口HTT

找到了罪犯

必须删除重载中的

从“http”导入{IncomingHttpHeaders}
键入Method=“POST”|“GET”|“PATCH”|“PUT”|“DELETE”
类型标题={
[键:字符串]:字符串
}
类型JSON={
[键:字符串]:任何
}
键入ResponseType=“buffer”|“json”|“text”
接口HTTPRequest{
方法:方法
url:string
标题?:标题
qs?:{
[键:字符串]:字符串
}
json?:json
}
接口HTTPRequestOptions{
标题?:标题
responseType?:responseType
是否重试?:{
限制?:数量
方法?:方法[]
}
}
接口HTTPResponse{
状态代码:编号
标题:Incomingttpheaders
正文:T
请求:HTTPRequest
}
类HTTP{
公职(
url:HTTPRequest[“url”],
json:HTTPRequest[“json”],
选项:HTTPRequestOptions&{responseType:“buffer”}
):承诺
公职(
url:HTTPRequest[“url”],
json:HTTPRequest[“json”],
选项:HTTPRequestOptions&{responseType:“json”}
):承诺
公职(
url:HTTPRequest[“url”],
json:HTTPRequest[“json”],
选项:HTTPRequestOptions&{responseType:“text”}
):承诺
公职(
url:HTTPRequest[“url”],
json?:HTTPRequest[“json”],
选项?:HTTPRequestOptions
):承诺
公职(
url:HTTPRequest[“url”],
json?:HTTPRequest[“json”],
选项?:HTTPRequestOptions
):承诺{
把这个还给我(
{
方法:“张贴”,
url:url,
json:json,
},
选择权
)
}
}
const httpInstance=new HTTP()
;(异步()=>{
//为什么'response'是'HTTPResponse'类型?
const response1=等待httpInstance.post(
"https://api.example.net/hello"
)
//以下行为符合预期(`response`属于`HTTPResponse`类型)
const response2=等待httpInstance.post(
"https://api.example.net/hello",
无效的
{
响应类型:“json”
}
)
})()

因为它匹配第一个重载,谢谢@AlekseyL。。刚刚发现在重载中使用
json?
options?
)是个问题。是的
使它们optional@AlekseyL. 希望提交并回答,还是我应该自己回答问题?