0

I have this code:

interface ISource extends IdModel {
  source_type_id: number;
  network_id: number;
  company_connection_id: number;
  feed_id: number;
  connection_id: number;
  feed_ids: number[];
  name: string;
  tag: string;
  additional_data: ISourceData;
  default_user_image?: File;
  hasNewImage?: boolean;
  next_update?: string;
  post_field_overrides?: {
    user_full_name?: string;
    user_screen_name?: string;
    url?: string;
    network_id?: number;
  };
}

save() {
   //converting this to string because I am saving this in my database.
   this.source.post_field_overrides = JSON.stringify(this.source.post_field_overrides);

}

If I don't convert that into string, that value that's being saved in my DB is [Object object].

If I also convert that into String, I am getting this error/warning:

TS2322: Type 'string' is not assignable to type '{ user_full_name?: string | undefined; user_screen_name?: string | undefined; url?: string | undefined; network_id?: number | undefined; } | undefined'.

Any help on this, please? Thanks in advance!

2
  • post_field_overrides? is neither string nor any... You should decide what you want it to be and write that I guess... Commented Dec 4, 2020 at 3:19
  • did you try this?: JSON.stringify(JSON.parse(this.source.post_field_overrides)); Commented Dec 4, 2020 at 3:23

2 Answers 2

2

this.source.post_field_overrides = JSON.stringify(this.source.post_field_overrides);

you have defined post_field_overrides as an object containing

{
    user_full_name?: string;
    user_screen_name?: string;
    url?: string;
    network_id?: number;
  }

in your typescript interface. now you want to replace the same atrribute with a string. this won't pass typescript's static type check.

if you need it to be a string , add another attribute in your interface.

or as Alexei Levenkov said ,use this instead to support both object and string

post_field_overrides?: string | {
    user_full_name?: string;
    user_screen_name?: string;
    url?: string;
    network_id?: number;
  };
Sign up to request clarification or add additional context in comments.

2 Comments

This one is really good! Thanks for this! Another problem is when I retrieve the data from the database, since it is a string, i have to JSON.parse() it the moment I retrieved it. This is how I parse it - this.source.post_field_overrides = JSON.parse(this.source.post_field_overrides);, The problem with that is I am getting again the error TS2345: Argument of type 'string | - how to do that?
I guess when you use JSON.parse to parse the string into object, typescript type checking will just see it as JSON Object not type { user_full_name: ...} you defined in the interface. So you may need to abstract this type definition as an independent type PostFieldOverride, use it in the interface as well as this place with JSON.parse(this.source.post_field_overrides) as PostFieldOverride
0

You are declaring post_field_overrides as object

 post_field_overrides?: {
    user_full_name?: string;
    user_screen_name?: string;
    url?: string;
    network_id?: number;
  };

if you need to use it as an String you need to declare as:

 post_field_overrides?: string;

Comments

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.