javascript - Typescript and converting Object to String - Stack Overflow

I'm converting a Object in Typescript to a string to store into a database.formData.survey_data

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
Add a ment  | 

1 Answer 1

Reset to default 4
formData.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).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742350649a4427448.html

相关推荐

  • javascript - Typescript and converting Object to String - Stack Overflow

    I'm converting a Object in Typescript to a string to store into a database.formData.survey_data

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信