简体   繁体   English

打字稿和将对象转换为字符串

[英]Typescript and converting Object to String

I'm converting a Object in Typescript to a string to store into a database.我正在将 Typescript 中的对象转换为字符串以存储到数据库中。

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[]'类型 'string' 不可分配给类型 'any[]'

What does that even mean?那有什么意思?

formData.survey_data = encodeURI(JSON.stringify(formData.survey_data));

Based on the code provided, I would assume that survey_data is type any[] .根据提供的代码,我假设survey_data的类型为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. TypeScript 是强类型的,即使 JavaScript 可能能够处理这种情况,也不允许您这样做。 (Because JavaScript isn't strongly typed you can assign any object to any property). (因为 JavaScript 不是强类型的,您可以将任何对象分配给任何属性)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM