When working with my app, I receive a response from an API that follows a specific pattern.
export interface IApiResponseBody<T = any> {
Error?: string;
Message?: string;
RowsAffected?: number;
Success?: boolean;
Data?: T;
RowCount?: number;
}
I have no trouble assigning the interface to the response and accessing its elements. However, I encounter an issue when trying to assign an interface to the Data
property using T
.
let myData: IApiResponseBody<IPerson> = {
Success: true,
Error: "",
Message: "",
RowsAffected: 9,
Data: [{name: 'Steve', age: 26, gender : 'male'},
{name: 'Susan', age: 21, gender : 'female'},
{name: 'SteveFrank', age: 29, gender : 'male'}]}
An error occurs whether I use an array in the Data
property or a simple document.
TSError: ⨯ Unable to compile TypeScript: index.ts:9:5 - error TS2559: Type '{ name: string; age: number; gender: string; }[]' has no properties in common with type 'IPerson'.
9 Data: [{name: 'Steve', age: 26, gender : 'male'}, ~~~~
How can I apply an interface to my Data
element, whether it's an array of documents or a single document?
You can view the code on Repl.it by following this link: