1

So I an calling this in angular

 savePat(obj: PatientData): Observable<IJsonSaveResult> {
    return this.http.post<IJsonSaveResult>(`${environment.serviceUrl}/patients/addpatient`, obj);
  }

my IJsonSaveResult is

export interface IJsonSaveResult {
    success:    boolean;
    errors:     any[];
    resultSet: any;

}

resultSet can be anything but i want to type it similar to .Net Generics

savePat(obj: PatientData): Observable<IJsonSaveResult(PatientData)> {
        return this.http.post<IJsonSaveResult>(`${environment.serviceUrl}/patients/addpatient`, obj);
      }

any ideas?

2 Answers 2

1

Typescript generics are remarkably similar to C# generics (no surprise, since Microsoft is behind Typescript).

You would declare the type of the resultSet as a generic parameter on the IJsonSaveResult interface:

export interface IJsonSaveResult<TResultSet> {
  success: boolean;
  errors: any[];
  resultSet: TResultSet;
}

And call it like this:

savePat(obj: PatientData): Observable<IJsonSaveResult<PatientData>> {
  const url = `${environment.serviceUrl}/patients/addpatient`;
  return this.http.post<IJsonSaveResult<PatientData>>(url, obj);
}
Sign up to request clarification or add additional context in comments.

1 Comment

I avoid Angular Material whenever possible. Good luck!
1

If I am understanding correctly, you could do something like:

export interface IJsonSaveResult<T> {
    success:    boolean;
    errors:     any[];
    resultSet: T[];
}

And then:

savePat(obj: PatientData): Observable<IJsonSaveResult<PatientData>> {
    return this.http.post<IJsonSaveResult<PatientData>>(`${environment.serviceUrl}/patients/addpatient`, obj);
}

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.