Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript and converting Object to String

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?

like image 899
Caleb Prenger Avatar asked Oct 03 '16 13:10

Caleb Prenger


1 Answers

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).

like image 184
kemiller2002 Avatar answered Oct 17 '22 12:10

kemiller2002