Description
TypeScript Version: 4.0.2
Search Terms:
type inference
reference types
Code
// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.
/// <reference types="node-fetch" />
import { request, IncomingHttpHeaders, RequestOptions } from "http";
import { URL } from "url";
interface NodeOptions {
url: URL;
options: RequestOptions;
}
function mapFetchParameters(
urlInfo: RequestInfo,
init?: RequestInit
): NodeOptions {
const reqUrl =
typeof urlInfo == "string" ? new URL(urlInfo) : new URL(urlInfo.url);
let headers = {};
let method: string = "";
let port = "80";
if (urlInfo instanceof Request) {
Object.assign(headers, urlInfo.headers);
method = urlInfo.method;
port = urlInfo.port.toString(); // ERROR??
}
if (init) {
method = init.method ? init.method : "";
}
const options = {
host: "localhost",
port: port,
path: "/posts",
method: method,
headers: headers,
};
const res = {
url: reqUrl,
options,
};
return res;
}
Expected behavior:
Compiler should find the port class property on the Request class.
The @types/node-fetch index.d.ts exports
export type RequestInfo = string | URLLike | Request;
export class Request extends Body {
method: string;
...
port?: number;
}
Actual behavior:
error TS2339: Property 'port' does not exist on type 'Request'.
71 port = urlInfo.port.toString();
Playground Link:
https://www.typescriptlang.org/play?#code/PQgEB4CcFMDNpgOwMbVAFwJ4AdoGcBeAIkQHsATaAWnnWQAsjRgA+AKAEsBbbUydUAG9QAJWgBHAK750AeWzoOpRHlABfULEikuoIvXTpsRANycefAcICqIgDLrN23UUmQANqbadE6BLABDVFAAOQpoeUVlVUE2UFA3dwAuUFs7M3jSBSUVFLEpGUicvDM1b1hJFCjEUC4A7AAxaDp6AAUAyACuZoQ8AAo4hI8ASURYUjyJaTx0UfGAGkGORA50AH5JgpnR1bYAShSwyiLooUHkaIEYcWsPUAJB+KxcUlgh9znSe4I9GchlgDmTDWoEQ0AA7ql7H1Ep89qAUmDIWkYSMxqQAHSJPYZUDuZqgejQAKUSCqH6CNS4-ECbroegUFJ-QH3PReeI00C8fisogADgADOzQBw3qiPuiRSp0AEUNBXqIpjJ4bF4vFZAAjABW0GQ6AxATweA4AMQfSJJN683enwxFtJeBxj1qzQZ5FZsPRGLpbtx8W5Ah+nvGGIDGPQpAAyuh-ogAX0nfEyvFRaA+stVirnT6KKyM-qc+6Qfnva7cykiMLk6ALtLQFlquSzmrCaQZhX3KRkAF3AyZkRFi2AykA4O1dgAvSK8BeDM8APs2XyClC2P4vbeikN2Sx1TzpdQDAm6q1YkUtdbu41-XstFd7iYOg3DUj6U2EA
Related Issues:
GitHub Link
https://github.com/paboulos/http-types