admin管理员组文章数量:1414604
I am trying to return a typed object using the Axios API. I am using the generic type to declare that I am returning an instance of GetSliceResponse
but unfortunately Axios seems to still return an object of type any.
My code looks like this
export class GetSliceResponse
{
Success: boolean;
}
Axios.post<GetSliceResponse>("myurl/Get", request).then(o => {
var expectedResult = (new GetSliceResponse()) instanceof GetSliceResponse;
//expectedResult = true;
var unexpectedResult = o.data instanceof GetSliceResponse;
//unexpectedResult = false;
});
The Http response is exactly what you would expect:
{"Success":false}
As the above code illustrates I can correctly create an instance of my type using the new
syntax but the Axios data property appears unaffected by the type declaration.
I am trying to return a typed object using the Axios API. I am using the generic type to declare that I am returning an instance of GetSliceResponse
but unfortunately Axios seems to still return an object of type any.
My code looks like this
export class GetSliceResponse
{
Success: boolean;
}
Axios.post<GetSliceResponse>("myurl/Get", request).then(o => {
var expectedResult = (new GetSliceResponse()) instanceof GetSliceResponse;
//expectedResult = true;
var unexpectedResult = o.data instanceof GetSliceResponse;
//unexpectedResult = false;
});
The Http response is exactly what you would expect:
{"Success":false}
As the above code illustrates I can correctly create an instance of my type using the new
syntax but the Axios data property appears unaffected by the type declaration.
1 Answer
Reset to default 4Just because something has the same properties as the class does not mean it is an instance of the class. In your case the response from the server is probably parsed using JSON.parse
which will create simple objects. Only objects created using new GetSliceResponse
will actually be instances of the class.
The type parameter to the post method is meant to help describe the shape of the response but will not actually change the runtime behavior (nor could it, genetics are erased during pilation).
This being said, you can still access the properties of the object as if the object was an instance of the class, the only thing that will not work is instanceof
and don't expect any method to be present.
If you want to make sure nobody uses instanceof
by mistake you can make the type am interface instead.
If you really need the class you can create an instance using new
and use Object.assign
to assign all fields
export class GetSliceResponse
{
Success: boolean;
}
Axios.post<GetSliceResponse>("myurl/Get", request).then(o => {
o = Object.assign(new GetSliceResponse(), o);
});
本文标签: javascriptAxios generic Post returns wrong type in TypeScriptStack Overflow
版权声明:本文标题:javascript - Axios generic Post returns wrong type in TypeScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745190321a2646880.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论