Types mismatch when using the Maybe monad + TypeScript
Closed this issue · 2 comments
Hey, thanks for the amazing work on this lib.
First of all, I'd like to point that this is more likely to be a mistake of mine than a real problem related to monet.js or TypeScript–then please point and I'll close the issue ASAP 😄 .
The problem I am facing is:
I have a GithubClient
class which basically takes care of processing data fetched from the Github API. The data is fetched using axios–a HTTP client abstraction lib–and I ended up using monet.js due to its Maybe monad since I wanted to properly handle Nothing
from responses.
The code goes something like this:
import axios, {
AxiosRequestConfig,
AxiosResponse,
AxiosError,
AxiosInstance
} from "axios";
import { Maybe } from 'Monet';
export class GithubClient {
private config: AxiosRequestConfig;
private client: AxiosInstance;
constructor() {
this.config = {
method: 'get',
baseURL: 'https://api.github.com',
headers: { 'Authorization': `token ${process.env.GITHUB_TOKEN}` },
//withCredentials: true
};
this.client = axios.create(this.config)
}
// ↓ ↓ ↓ focus here ↓ ↓ ↓
public async getAvatarUrl(username: string) {
try {
const response: AxiosResponse = await this.client.get(`users/${username}`)
return this.handleResponse(response)
} catch (e) {
this.handleError(e);
}
}
private handleResponse (response: AxiosResponse): Maybe<string> {
return Maybe.fromNull(response.data.avatar_url)
}
private handleError(error: AxiosError) {
(error.response)
? ( console.log(error.response.data),
console.log(error.response.status),
console.log(error.response.headers) )
: console.log('Error: ', error.message);
}
}
But the compiler throws me:
src/Server.ts(31,28): error TS2345: Argument of type 'Maybe<string>' is not assignable to parameter of type 'String'.
Property 'charAt' is missing in type 'Maybe<string>'.
I don't really know from where String
is being inferred nor from where charAt
comes–I couldn't even find this on monet.js source.
Any ideas?
@ythecombinator on my side this code transpiles correctly and getAvatarUrl
method has correct type Promise<Maybe<string>>
.
How do you use getAvatarUrl
?
@ythecombinator - error message informs that somewhere in the code Maybe<string>
returned (probably) from getAvatarUrl
is passed to a function which expects string
.
Maybe you can share code around line 31 from src/Server.ts
file ?