In my implementation, I have a callback called didReceiveResponse
within a class that extends RESTDataSource. In this method, I return null
when the response status is 404. However, due to the typing definition of
RESTDataSource.didReceiveResponse
, it seems this approach is considered invalid.
async didReceiveResponse<T>(res: Response, req: Request): Promise<T | null> {
if (res.status === 404) {
return null;
}
return super.didReceiveResponse(res, req);
}
The issue arises when using --strict-null-checks
in TypeScript:
TS2416: Property 'didReceiveResponse' in type 'APIDataSource' cannot be assigned to the same property in the base type 'RESTDataSource<ResolverContext>'.
The type '<T>(res: Response, req: Request) => Promise<T | null>' is not compatible with '<TResult = any>(response: Response, _request: Request) => Promise<TResult>'.
The type 'Promise<TResult | null>' cannot be assigned to 'Promise<TResult>'.
'TResult | null' cannot be assigned to 'TResult'.
'null' cannot be assigned to 'TResult'.
Is there a way to address this typing issue while still being able to return null
, without having to turn off the compiler or disable strict null checking?