TypeScript - Undefined

Sample

1
2
3
4
5
6
7
8
9
getAllPolicyAttachmentsByPolicyId(policyId: number | undefined): Observable<Result<PolicyAttachment[]>> {
console.log(`getAllPolicyAttachmentsByPolicyId - ${policyId}`);
if (policyId === undefined) {
return this.http.get<Result<PolicyAttachment[]>>(this.apiUrl);
} else {
return this.http.get<Result<PolicyAttachment[]>>(`${this.apiUrl}/getallbypolicyid/${policyId}`);
}

}

In this example policyId will either be a number, or it will be undefined.

An if statement checks, and calls different API depending on the logic.

References