admin管理员组文章数量:1333451
I'm converting a Object in Typescript to a string to store into a database.
formData.survey_data = encodeURI(JSON.stringify(formData.survey_data));
The output works,in the browser but typescript insists I have an error.
Type 'string' is not assignable to type 'any[]'
What does that even mean?
I'm converting a Object in Typescript to a string to store into a database.
formData.survey_data = encodeURI(JSON.stringify(formData.survey_data));
The output works,in the browser but typescript insists I have an error.
Type 'string' is not assignable to type 'any[]'
What does that even mean?
Share Improve this question asked Oct 3, 2016 at 13:28 Caleb PrengerCaleb Prenger 2,0774 gold badges15 silver badges13 bronze badges 2-
What is the type of
survey_data
? – Whymarrh Commented Oct 3, 2016 at 13:30 - I defined it as an array, that was my issue. I defined it as an array and tried to make it a string. – Caleb Prenger Commented Oct 3, 2016 at 13:42
1 Answer
Reset to default 4formData.survey_data = encodeURI(JSON.stringify(formData.survey_data));
Based on the code provided, I would assume that survey_data
is type any[]
. You are serializing your object and trying to assign it to that property. TypeScript is strongly typed and won't allow you to do that even though JavaScript may be able to handle that scenario. (Because JavaScript isn't strongly typed you can assign any object to any property).
本文标签: javascriptTypescript and converting Object to StringStack Overflow
版权声明:本文标题:javascript - Typescript and converting Object to String - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742350648a2458424.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论