1

I am having a tough time to create an interface model to match the data come form Firebase. My web storm keep throwing type error for me. Here is the structure.

I have a model interface like this:

   export interface Foundation {
            channelInfo?: {
              youTubeSubs?: number,
              instagramFollowers?: number,
              facebookLikes?: number,
              snapchatFollowers?:number,
              twitterFollowers?:number,
              genreOther: string,
              genre: Array<string>
            };
  }

In Firebase, the data structure is almost the same:

            channelInfo: {
              youTubeSubs: string,
              instagramFollowers: string,
              facebookLikes: string,
              snapchatFollowers: string,
              twitterFollowers: string,
              genreOther: string,
              genre: [...]
            };

But all the number type is string as that is how the Firebase store the data. Also, on Firebase it can be missing data like snapchatFollowers and twitterFollowers can not be on Firebase (if the user never input any data). So if I = the Foundation variable to a Firebase observable return of this

            channelInfo: {
              youTubeSubs: string,
              instagramFollowers: string,
              facebookLikes: string,
              genreOther: string,
              genre: [...]
            };

It throw error: ERROR in /Users/hughred22/nodeapp/dev/YouTube-BAP-app/WebAdminPanel/src/app/user-page/user-page.component.ts (84,189): Property 'twitterFollowers' does not exist on type '{ youTubeSubs: string; instagramFollowers: string; facebookLikes: string; genreOther: string; gen...'.) /Users/hughred22/nodeapp/dev/YouTube-BAP-app/WebAdminPanel/src/app/user-page/user-page.component.ts (84,250): Property 'snapchatFollowers' does not exist on type '{ youTubeSubs: string; instagramFollowers: string; facebookLikes: string; genreOther: string; gen...'.)

If I make the variable ANY type instead of my Foundation type, it will work. I can I create a correct interface model to take in optional Firebase data so I won't have this kind of typescript error?

2 Answers 2

2

Interface properties can have multiple possible types.

export interface ChannelInfo {
    youTubeSubs?: number | string,
    instagramFollowers?: number | string,
    facebookLikes?: number | string,
    snapchatFollowers?: number | string,
    twitterFollowers?: number | string,
    genreOther: string,
    genre: string[]
}

export interface Foundation {
    channelInfo?: ChannelInfo
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh wow. This just take care of the error nicely. Great tips!!
0
interface Foundation {
            channelInfo?: {
              youTubeSubs?: number,
              instagramFollowers?: number,
              facebookLikes?: number,
              snapchatFollowers?:number,
              twitterFollowers?:number,
              genreOther: string,
              genre: Array<string>
            };
  }

1 Comment

Welcome to Stack Overflow! While this code 🧑‍💻 snippet may be the solution, including an explanation 🧑‍🏫 really helps to improve the quality of your post. Remember that you are answering the question for readers in the future 🤝, and those people might not know the reasons for your code suggestion.

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.