Reputation: 299
I have an interface that looks like this
interface CallBackHandler{
(err: Error, response: {statusCode: number, body: object}):void
}
and i want to apply that interface to the callback of this
request({
url: url,
method: "GET",
withCredentials: false,
json: true,
headers: headers
}, (err, response) => {
this.handleResponse(err, response, resolve, reject);
});
but im getting a error saying the function must return something if the return type is not void when i add the interface
(err, response): CallBackHandler => {
this.handleResponse(err, response, resolve, reject);
}
what is the correct way to apply this interface?
Upvotes: 4
Views: 289
Reputation: 59397
If request()
already has the type signature:
function request(options, callback: CallBackHandler) {
...
}
Then you shouldn't need to do anything, as the callback you provide will be type-checked accordingly.
If that function doesn't already have that type signature, and you want to manually cast your callback to CallBackHandler
, then you will need to wrap the callback function in parentheses and cast that expression, like so:
request({
...etc
}, ((err, response) => {
this.handleResponse(err, response, resolve, reject);
}) as CallBackHandler);
Upvotes: 1
Reputation: 9102
You can have something like
request({
url: url,
method: "GET",
withCredentials: false,
json: true,
headers: headers
}, (err: Error, response: {statusCode: number, body: object}) => {
this.handleResponse(err, response, resolve, reject);
});
Upvotes: 0