I have the following code for making a request using the https
module.
export const makeRequest = (requestOptions: RequestOptions, body?: string): Promise<string> =>
new Promise((resolve, reject) => {
const req = https.request(requestOptions, (res: IncomingMessage): void => {
// TODO: This assumes the client wants a string--consider making generic
res.setEncoding("utf8");
let data = "";
res.on("data", chunk => data += chunk);
res.once("end", (): void => resolve(data));
});
req.once("error", error => reject(error));
if (body) {
req.write(body);
}
req.end();
});
I'd like to make this generic in the type that's returned but to default to string
, so I made changes and now have the following (which does not compile).
export interface OnDataAccumulator<T> {
encoding: string;
listener: (chunk: any) => void;
result: () => T;
}
const makeOnDataStringAccumulator: () => OnDataAccumulator<string> = () => {
let data = "";
return {
encoding: "utf8",
listener: (chunk: any) => data += chunk,
result: () => data
};
};
export const makeRequest = <T = string>(requestOptions: RequestOptions,
accumulator: OnDataAccumulator<T> = makeOnDataStringAccumulator(),
body?: string): Promise<T> =>
new Promise((resolve, reject) => {
const req = https.request(requestOptions, (res: IncomingMessage): void => {
res.setEncoding(accumulator.encoding);
res.on("data", accumulator.listener);
res.once("end", (): void => resolve(accumulator.result()));
});
req.once("error", error => reject(error));
if (body) {
req.write(body);
}
req.end();
});
I assumed that the unlock for this was the type parameter <T = string>
but instead I see the typescript error
Type 'OnDataAccumulator<string>' is not assignable to type 'OnDataAccumulator<T>'.
Type 'string' is not assignable to type 'T'.
'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
- What's the correct approach for this?
- From a consumer's perspective, would this be more complete for it to be generic in the
body
type? - Similarly, would it be more complete to provide an impl for the callbacks in each
req.write
andreq.end
? - Is "accumulator" the correct term for this object?