[英]Property 'host' does not exist on type 'ProxyTargetUrl' Error. Even though i get IntelliSense in JS File
I have the following code in a .ts
file: 我在.ts
文件中有以下代码:
const connection = {
host: options.target.host
};
options
are of type httpProxy.ServerOptions
and target
is of type ProxyTargetUrl
options
的类型为httpProxy.ServerOptions
, target
的类型为ProxyTargetUrl
The error i get is the following: 我得到的错误如下:
Property 'host' does not exist on type 'ProxyTargetUrl'.
Property 'host' does not exist on type 'string'.
These options are part from the http-proxy
npm module. 这些选项是http-proxy
npm模块的一部分。 The newest typings are installed. 最新的类型已安装。 "@types/http-proxy": "^1.16.2"
When i follow the declaration i see this: 当我遵循声明时,会看到以下内容:
type ProxyTargetUrl = string | url.Url;
-- -
interface Url extends UrlObjectCommon {
port?: string;
query?: string | null | ParsedUrlQuery;
}
-- -
interface UrlObjectCommon {
auth?: string;
hash?: string;
host?: string;
hostname?: string;
href?: string;
path?: string;
pathname?: string;
protocol?: string;
search?: string;
slashes?: boolean;
}
Why do i get this error? 为什么会出现此错误? When i copy my code over in a JS File i get IntelliSense for the host
property and the code also works perfectly fine at runtime. 当我将代码复制到JS文件中时,我获得了IntelliSense的host
属性,并且代码在运行时也运行良好。 So the typings are correct. 因此键入是正确的。 What did i miss? 我错过了什么?
The problem is that in your union type type ProxyTargetUrl = string | url.Url
问题是在您的联合类型中, type ProxyTargetUrl = string | url.Url
type ProxyTargetUrl = string | url.Url
, only Url
has field host
defined. type ProxyTargetUrl = string | url.Url
,只有Url
定义了字段host
。
To access this field, you would need a type assertion: (<Url>options.target).host
. 要访问此字段,您需要类型断言: (<Url>options.target).host
。
See this Github issue for more information. 有关更多信息,请参见此Github问题 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.