Curious about the correct way to utilize the HTTP Client in Angular 4 with type checking? According to the official documentation at https://angular.io/guide/http, here is an example:
Imagine we have a Cake:
export interface Cake {
numberOfCandles: number;
diameter: number
}
and a CakeService:
@Injectable()
export class CakeService {
public getCake(cakeUrl: string): Observable<Cake> {
return this.http.get<Cake>(cakeUrl);
}
}
Seems straightforward. I decided to run a test to observe its functionality:
it('should get one cake', inject([CakeService, HttpTestingController], (http: CakeService, httpMock: HttpTestingController) => {
http
.getCake('testurl')
.subscribe((data: Cake) => {
expect(data.numberOfCandles).toBe('Test');
});
const req = httpMock.expectOne('testurl');
expect(req.request.method).toEqual('GET');
req.flush({
numberOfCandles: 'Test'
});
httpMock.verify();
}));
To my surprise, the test passed. Shouldn't the type check detect that a string was provided instead of a number? Am I missing a step outlined in the documentation? It appears like there is no runtime typechecking going on here. How can I implement this and is it even necessary? Thank you!