简体   繁体   English

类型“ ProxyTargetUrl”上不存在属性“主机”错误。 即使我在JS文件中获得了IntelliSense

[英]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.ServerOptionstarget的类型为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.

相关问题 即使声明了类型,属性也不存在于类型上 - Property does not exist on type even though type is declared 打字稿“TS2339:属性 &#39;X&#39; 不存在于类型 &#39;Y&#39;”错误,即使类型已定义 - TypeScript "TS2339: Property 'X' does not exist on type 'Y'" error even though type is defined 类型“Window &amp; typeof globalThis”上不存在属性。 即使它是在定义文件中定义的 - Property does not exist on type 'Window & typeof globalThis'. Even though it is defined in definition file 即使包含类型文件,Typescript也会抛出属性上不存在的属性 - Typescript throws a property does not exist on type even with type file included “PromiseSettledResult”类型上不存在属性“原因”<never> &#39;。 即使我已经过滤了已解决的结果,所以只剩下拒绝 - Property 'reason' does not exist on type 'PromiseSettledResult<never>'. Even though I have filtered the settled results so that only rejection remain &#39;FunctionComponent 类型不存在属性 Y<Interface> &#39; 即使接口包含属性 Y - Property Y does not exist on type 'FunctionComponent<Interface>' even though Interface contains property Y 具有联合类型的扩展接口抛出“属性在类型上不存在”错误。 如何解决这个问题? - Extended interfaces with union type is throwing "property does not exist on type" error. How to fix this? 从 JS 切换到 TS 时类型错误上不存在属性 - Property does not exist on type error when switching from JS to TS 发布应用程序时出现错误。 错误是属性json在类型对象上不存在 - I get a error while publishing a app. The error is property json does not exist on type object 我正在使用 Angluar 的 Lateset 版本,我收到此错误属性“值”在类型“EventTarget”上不存在 - I'm using the Lateset version of Angluar and I get this error Property 'value' does not exist on type 'EventTarget'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM