6

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?

2
  • What is the type of survey_data? 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. Commented Oct 3, 2016 at 13:42

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

So do you mean because I assigned it to something else before hand, it throws an error because I try to reassign it?
Somewhere in the code formData.survey_data has a has a class/property definition which is specifies that it is of type any[]. If you want to change it you have to find that.
Figured it out! I defined formData.survey_data as an array earlier and not a string. Thanks for your help!
glad i could help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.