Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

angular typescript declare generic with empty object

I am trying to declare following variable in angular 12

private subject = new BehaviorSubject<ApiResponse>({});

I get error

Argument of type '{}' is not assignable to parameter of type 'ApiResponse'
Type '{}' is missing the following properties from type 'ApiResponse'

The class ApiResponse has following properties

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

export interface ApiResponse {
  success: boolean
  message: string
  exception: any
  totalRowCount: number
}

If I declare each property explicitly, it works without error
Is there a way to declare an empty object without declaring each property explicitly ?

>Solution :

Use ? for optional properties

export interface ApiResponse {
  success?: boolean;
  message?: string;
  exception?: any;
  totalRowCount?: number;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading