As a newcomer to Angular and web development, I apologize in advance if my terminology or explanations are not accurate. Here is my issue:
I am working with Angular 9 and making http requests (get and post) to an API using the HttpModule. The response object (and post object) is a JSON object that contains an array of variable objects:
inputs: [
{
id: string,
sT: string,
stl: string,
mCC: number
},
{
id: string,
sT: string,
val: string,
dT: string
}
]
I have learned that it's common practice to create interfaces for response objects in order to access their properties by name. I have done this for all other properties, including nested ones, but I'm facing an issue with the array above where the objects do not have the same attributes. So, I created an interface for 'Input' with optional attributes like this:
// Single interface with optional attributes
export interface Input {
id: string;
sT: string;
dT?: string;
val?: string;
mCC?: number;
stl?: string;
}
However, I feel that this may not be the standard way of typing this type of object. What would be the recommended practice to handle this situation 'Angularish way'? Is this more of a TypeScript question?
I would appreciate any guidance on best practices for handling this type of object, as I anticipate encountering similar scenarios frequently in the future.