I'm using fp-ts and I have a function that returns either an HttpError object or a string:
async getPreferencesForUserId(userId: string): Promise<Either<HttpResponseNotFound, string>> {
const preferences = await getRepository(Preference).findOne({ userId });
return preferences ? right(preferences.preferenceMap) : left(new HttpResponseNotFound({ code: 404, message: 'Could not find preferences' }));
}
I would like to call this function in another file like this:
const preferenceMapAsJsonStringOrError: Either<HttpResponseNotFound, string> = await this.preferenceService.getPreferencesForUserId(userId);
const response: HttpResponseOK | HttpResponseNotFound = pipe(preferenceMapAsJsonStringOrError, fold(
e => e,
r => new HttpResponseOK(r)
));
response.setHeader('content-type', 'application/json');
return response;
This is basically how I'd do it in Scala. (with the exception that fold
is a method on the Either type, not a standalone function - so here I'm using the pipe
helper)
The problem is, I'm getting an error from ts-server:
Type 'HttpResponseOK' is missing the following properties from type 'HttpResponseNotFound': isHttpResponseNotFound, isHttpResponseClientError
And
node_modules/fp-ts/lib/Either.d.ts:129:69
129 export declare function fold<E, A, B>(onLeft: (e: E) => B, onRight: (a: A) => B): (ma: Either<E, A>) => B;
~~~~~~~~~~~
The expected type comes from the return type of this signature.
I can get around this issue by doing this in a much more imperative way:
const preferenceMapAsJsonStringOrError: Either<HttpResponseNotFound, string> = await this.preferenceService.getPreferencesForUserId(userId);
if (isLeft(preferenceMapAsJsonStringOrError)) {
return preferenceMapAsJsonStringOrError.left;
}
const response = new HttpResponseOK(preferenceMapAsJsonStringOrError.right);
response.setHeader('content-type', 'application/json');
return response;
But I pretty much lose the benefit of using an Either at that point.