我试图同时指定JSON主体和响应类型(它们是不同的,在某些示例中,两者不知何故相同)。
错误消息:
参数类型 '{ grant_type: string; refresh_token: string; }' 不能赋值给类型 'AuthResponse' 的参数。 对象文字只能指定已知属性,而'grant_type'不存在于类型'AuthResponse'中。
这是我定义为我的类型的响应:
错误消息:
参数类型 '{ grant_type: string; refresh_token: string; }' 不能赋值给类型 'AuthResponse' 的参数。 对象文字只能指定已知属性,而'grant_type'不存在于类型'AuthResponse'中。
这是我定义为我的类型的响应:
export interface AuthResponse {
token: string;
token_type: string;
access_token: string;
access_token_expires_at: string;
refresh_token: string;
refresh_token_expires_at: string;
}
axios的使用:
axios
.post<AuthResponse>(
secrets.authURL,//Defined in enclosing class
{
grant_type: this.grantType,//Defined in enclosing class
refresh_token: this.authPayload,//Defined in enclosing class
},//<-- Defined as data and conflicts with AuthResponse
{
headers: {
Authorization: this.getAuthHeader(),//Defined in enclosing class
"Content-Type": this.contentType,///Defined in enclosing class
},
}
)
.then((axiosResp) => {
const response = axiosResp.data;//<-- Not the type I'd expect
});
axiosResp.data
的类型是{grant_type: ..., refresh_token: ...}
,而不是AuthResponse
。
从普通JS示例中可以看出,数据是请求的主体,但不确定为什么会被强制视为响应相同,所以我一定做错了什么。
编辑/回答: 正如@jrsharpe评论所指出的那样,这确实是一个bug,他们刚刚发布了v0.23.0。它修复了bug #4116。所以只需升级到0.23.0+即可。